2012-07-14 48 views
0

我試圖使用Twitter Search來獲取推文標籤。通過file_get_contents通過#標籤獲取推文

由於我只是要讀一個JSON文件,我用file_get_contents,而不是像cURL

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

但是,當我在我的本地主機上運行的代碼,它顯示了以下錯誤。有關它的任何想法?

Warning:file_get_contents(http://search.twitter.com/search.json)[function.file-get-contents]:未能打開流:無法找到套接字傳輸「ssl」 - 你有沒有在配置PHP時忘記啓用它?

+0

爲什麼1張題外話接近的選票?對於SO – gopi1410 2012-07-14 15:35:11

回答

1

在除FGC本地文件之外的每種情況下,最好使用卷繞FGC。

啓用本地主機上的捲曲如果未啓用extension=php_curl.dll(活主機的99.9%的捲曲啓用。)

試試這個:

<?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; 
} 
?> 
+0

這似乎是一個完全有效的問題那麼我在本地主機上啓用了curl,會測試它嗎?但是**爲什麼**比這裏的FGC捲曲更好,我們只需要閱讀一個文件。我認爲在這種情況下兩者相等 – gopi1410 2012-07-14 15:40:51

+0

沒有FGC只是fopen函數的包裝,因爲遠程操作的捲曲速度接近兩倍。 – 2012-07-14 15:44:15

+1

比較:http://www.anildewani.com/benchmarking-file_get_contents-and-curl-whose-faster/加上捲曲更加健壯 – 2012-07-14 15:46:20

-1

我強烈建議你使用捲曲,而不是的file_get_contents。但是,如果你不能動搖,那麼這裏是我讀到的問題:

它看起來像你沒有在你的PHP啓用SSL。我有點驚訝,你使用了一個http:// URL並且得到了那個錯誤。如果您使用的是https://網址,我希望您能得到該錯誤。

不管怎麼說,打開你的php.ini文件並取消以下行,然後重新啓動您的Web服務器,你應該是好去:

extension=extensions/php_openssl.dll 
+0

k,但爲什麼cURL的建議超過fgc,有什麼具體優勢? – gopi1410 2012-07-14 15:42:15

+0

在我的php.ini中沒有這樣的擴展名! – gopi1410 2012-07-14 15:46:19