0

I was trying to use the Twitter Search to get tweets of a hashtag.

Since I just have to read a json file, I used file_get_contents instead of cURL like

file_get_contents('http://search.twitter.com/search.json?q=%23trends');

But when I run the code in my localhost, it shows the following error. Any idea about it?

Warning: file_get_contents(http://search.twitter.com/search.json) [function.file-get-contents]: failed to open stream: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?

4

1 回答 1

1

在除 FGC 本地文件之外的所有情况下,最好在 FGC 上使用 curl。

如果未启用,则在 localhost 上启用 curl extension=php_curl.dll(99.9% 的活动主机启用了 curl。)

尝试这个:

<?php 
//Grab the twitter API with curl
$result = curl_get('http://search.twitter.com/search.json?q=%23trends');

//Decode the json result
//into an Object
$twitter = json_decode($result);
//into an Array
$twitter = json_decode($result,true);

//Debug
print_r($twitter);

function curl_get($url){
    $return = '';
    (function_exists('curl_init')) ? '' : die('cURL Must be installed!');

    $curl = curl_init();
    $header[0] = "Accept: text/xml,application/xml,application/json,application/xhtml+xml,";
    $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, 'TwitterAPI.Grabber (http://example.com/yoursite)');
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, true);
    curl_setopt($curl, CURLOPT_PROXY, 'http://proxy_address/');
    curl_setopt($curl, CURLOPT_PROXYUSERPWD, 'username:password');
    curl_setopt($curl, CURLOPT_PROXYPORT, 'portno');

    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}
?>
于 2012-07-14T15:38:57.723 回答