【问题标题】:Getting tweets by hashtags via file_get_contents通过 file_get_contents 通过主题标签获取推文
【发布时间】:2012-07-14 03:48:44
【问题描述】:

我试图使用Twitter Search 来获取主题标签的推文。

由于我只需要读取一个 json 文件,因此我使用 file_get_contents 而不是 cURL 之类的

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

但是当我在本地主机中运行代码时,它显示以下错误。有什么想法吗?

警告:file_get_contents(http://search.twitter.com/search.json) [function.file-get-contents]:无法打开流:无法找到套接字传输“ssl” - 确实配置 PHP 时忘记启用?

【问题讨论】:

  • 为什么 1 票赞成离题?对于 SO 来说,这似乎是一个完全有效的问题

标签: php twitter tweets file-read


【解决方案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;
}
?>

【讨论】:

  • 好吧,我在 localhost 上启用了 curl,可以测试一下吗?但是 why 这里 curl 比 FGC 好,我们只需要读取一个文件。我认为在这种情况下两者都是平等的
  • no FGC 只是 fopen 函数的包装,对于远程操作 curl 的速度接近两倍。
  • 比较:anildewani.com/… 加上 curl 更健壮
  • +1 谢谢,我将使用 cURL!但是当我使用上面的 curl 代码时,它显示了我大学局域网中使用的认证页面,即使我已经通过了认证!仍然在我输入凭据后,它会重定向到 localhost。对此有任何想法吗?
  • 这也许就是 FGC 需要 ssl 的原因,我不确定我能不能帮你,抱歉:s
猜你喜欢
  • 1970-01-01
  • 2022-01-12
  • 2020-10-25
  • 1970-01-01
  • 2013-12-01
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
相关资源
最近更新 更多