digioz Posted February 3, 2012 Share Posted February 3, 2012 Hello PHP Freaks! I have the following PHP Curl code which is supposed to fetch the twitter timeline feed for a specific username and parse and display it on a page, but for some reason Curl is unable to fetch data from twitter: <?php function get_data($url) { $ch = curl_init(); $timeout = 5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $data = curl_exec($ch); curl_close($ch); return $data; } $json = get_data("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=digioz&count=2"); if ($json != false) { $obj = json_decode($json); foreach($obj as $var => $value) { echo "Message number: $var <br/>"; echo "Name: " . $obj[$var]->user->name; echo "Handle: " . $obj[$var]->user->screen_name . "<br/>"; echo "Message: " . $obj[$var]->text; echo "Created" . $obj[$var]->created_at . "<br/>"; echo "URL" . $obj[$var]->user->url . "<br/>"; echo "Location" . $obj[$var]->user->location . "<br/>"; echo "<br/>"; } } else { echo "Could not fetch Twitter Data"; } ?> Anyone have any idea why the data is not being fetched? If I copy and paste the URL into my browser window it returns results just fine, so I know the problem is not with the URL. Thanks, Pete Quote Link to comment https://forums.phpfreaks.com/topic/256342-curl-fetch-of-twitter-timeline-feed-not-working/ Share on other sites More sharing options...
Cobra23 Posted February 3, 2012 Share Posted February 3, 2012 Have you assigned the URL to : $url Quote Link to comment https://forums.phpfreaks.com/topic/256342-curl-fetch-of-twitter-timeline-feed-not-working/#findComment-1314125 Share on other sites More sharing options...
digioz Posted February 3, 2012 Author Share Posted February 3, 2012 Have you assigned the URL to : $url Yeah, on this line: curl_setopt($ch,CURLOPT_URL,$url); The curl function "get_data" is fetching content from other URL fine, just doesn't work with twitter. Pete Quote Link to comment https://forums.phpfreaks.com/topic/256342-curl-fetch-of-twitter-timeline-feed-not-working/#findComment-1314126 Share on other sites More sharing options...
PFMaBiSmAd Posted February 3, 2012 Share Posted February 3, 2012 Are you using https with these other URL's that work? For php to be able to make a https request, you need to have OpenSSL installed. Quote Link to comment https://forums.phpfreaks.com/topic/256342-curl-fetch-of-twitter-timeline-feed-not-working/#findComment-1314130 Share on other sites More sharing options...
Cobra23 Posted February 3, 2012 Share Posted February 3, 2012 And from that line it's getting the $url from the first line of the function. BUT where is that $url in the function line getting that URL address from ? Quote Link to comment https://forums.phpfreaks.com/topic/256342-curl-fetch-of-twitter-timeline-feed-not-working/#findComment-1314131 Share on other sites More sharing options...
PaulRyan Posted February 3, 2012 Share Posted February 3, 2012 Its a commonly overlooked issue, simply using the following line will fix it: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); Put that inside your function below: curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/256342-curl-fetch-of-twitter-timeline-feed-not-working/#findComment-1314134 Share on other sites More sharing options...
digioz Posted February 3, 2012 Author Share Posted February 3, 2012 Are you using https with these other URL's that work? For php to be able to make a https request, you need to have OpenSSL installed. You are absolutely correct, but I don't have OpenSSL installed on my development machine though. Its a commonly overlooked issue, simply using the following line will fix it: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); Put that inside your function below: curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); Regards, PaulRyan. Thank you very much Paul! This worked like a charm. Much appreciated! Pete Quote Link to comment https://forums.phpfreaks.com/topic/256342-curl-fetch-of-twitter-timeline-feed-not-working/#findComment-1314136 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.