douchuopiao9002 2013-11-13 16:31
浏览 38
已采纳

用PHP模仿cURL

I have a curl request which works perfectly fine on my shell. But I tried to do the same on PHP.

Here's my code: (The first comment being the curl syntax which works perfectly fine.But if i try that in PHP. the curl_exec() cannot happen? What mistake am i doing here?

<?php 
/* 
curl --get 'https://api.twitter.com/1.1/search/tweets.json' 
--data 'q=%23mozilla' 
--header 'Authorization: OAuth oauth_consumer_key="my-key", oauth_nonce="my-nonce", oauth_signature="my-signature", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1384358463", oauth_token="my-token", oauth_version="1.0"'
*/
$header= 'Authorization: OAuth oauth_consumer_key="my-key", oauth_nonce="my-nonce", oauth_signature="my-signature", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1384358463", oauth_token="my-token", oauth_version="1.0"';
$options = array( 
    CURLOPT_HTTPHEADER => $header,
    CURLOPT_HEADER => false,
    CURLOPT_URL => 'https://api.twitter.com/1.1/search/tweets.json?q=%23mozilla',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 10,
);



$ch = curl_init() or die('Cannot Curl :/');
curl_setopt_array($ch, $options);
$return = curl_exec($ch) or die(curl_error($ch)); //Dies here with: "No URL set!"
curl_close($ch);
echo $return;
  • 写回答

1条回答 默认 最新

  • doudao2954 2013-11-14 04:52
    关注

    (Putting my comment as an answer)

    According to the documentation, CURLOPT_HTTPHEADER should have an array, not a string.

    You want the below:

    $header= array('Authorization: OAuth oauth_consumer_key="my-key", oauth_nonce="my-nonce", oauth_signature="my-signature", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1384358463", oauth_token="my-token", oauth_version="1.0"');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 simulink单相桥式整流电路
  • ¥35 问问51单片机流水灯的代码该怎么写
  • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
  • ¥15 stata webuse报错
  • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
  • ¥15 如何利用AI去除图片中的竹架子
  • ¥15 python 写个基金爬取的代码,自动卖出功能
  • ¥15 Linux系统启动不起来
  • ¥15 为什么运行仿真数码管不亮(语言-c语言)
  • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导