用PHP模仿cURL

时间:2013-11-13 16:31:01

标签: php curl

我有一个curl请求,在我的shell上运行得非常好。但我试图在PHP上做同样的事情。

这是我的代码:(第一个评论是curl语法完美正常。但是如果我在PHP中尝试它。curl_exec()不能发生?我在这做什么错?

<?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 个答案:

答案 0 :(得分:1)

(把我的评论作为答案)

According to the documentationCURLOPT_HTTPHEADER应该有一个数组,而不是一个字符串。

您需要以下内容:

$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"');