user1048676
user1048676

Reputation: 10066

Using cURL to get a URL that is returning JSON

All, I have the following code:

$c = curl_init();
curl_setopt($c, CURLOPT_URL, "https://api.twitter.com/1/statuses/user_timeline.json?screen_name={$username}&count={$how_many}");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($c);
curl_close($c);
if ( ! empty( $contents ) ) {
    // Decode it.
            echo "it is in here";
    $tweet = json_decode( $contents );
}

This code never gets into the if statement because it isn't returning any results. Any idea on how to get this to return all of the results?

Thanks!

Upvotes: 0

Views: 624

Answers (2)

Roman Newaza
Roman Newaza

Reputation: 11700

Try this:

$c = curl_init();
curl_setopt($c, CURLOPT_URL, "https://api.twitter.com/1/statuses/user_timeline.json?screen_name={$username}&count={$how_many}");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($c);

if(curl_exec($ch) === false)
    echo 'ERROR: '.curl_error($ch);

curl_close($c);

Upvotes: -1

Phil
Phil

Reputation: 165059

Looks like your server doesn't trust Twitter's certificate authority (you're probably getting an SSL error but not seeing it due to your error reporting settings).

Follow this guide to get it working - http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

Upvotes: 2

Related Questions