cipher
cipher

Reputation: 2484

Mimicing cURL in PHP

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;

Upvotes: 0

Views: 249

Answers (1)

UltraInstinct
UltraInstinct

Reputation: 44474

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

Upvotes: 1

Related Questions