【发布时间】:2015-03-13 04:45:33
【问题描述】:
我使用这些函数来获取社交分享计数:
function get_tweets($url) {
$json_string = $this->file_get_contents_curl('http://urls.api.twitter.com/1/urls/count.json?url=' . rawurlencode($url));
$json = json_decode($json_string, true);
return isset($json['count'])?intval($json['count']):0;
}
function get_fb($url) {
$json_string = $this->file_get_contents_curl('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls='.rawurlencode($url));
$json = json_decode($json_string, true);
return isset($json[0]['total_count'])?intval($json[0]['total_count']):0;
}
function get_pinterest($url) {
$return_data = $this->file_get_contents_curl('http://api.pinterest.com/v1/urls/count.json?url='.rawurlencode($url));
$json_string = preg_replace('/^receiveCount((.*))$/', "\1", $return_data);
$json = json_decode($json_string, true);
return isset($json['count'])?intval($json['count']):0;
}
private function file_get_contents_curl($url){
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
$cont = curl_exec($ch);
if(curl_error($ch))
{
die(curl_error($ch));
}
return $cont;
}
function get_plusones($url) {
$contents = file_get_contents('https://plusone.google.com/_/+1/fastbutton?url='.rawurlencode( $url ));
preg_match( '/window\.__SSR = {c: ([\d]+)/', $contents, $matches );
return isset($matches[0]) ? (int)str_replace('window.__SSR = {c: ', '', $matches[0]) : 0;
}
自 2 天以来,它不再适用于 twitter 和 google,我收到此错误:
failed to open stream: Connection timed out
我认为我的 ip 已被 google 和 twitter 禁止,但我不确定。 有人可以证实我的假设吗?
如果我通过 javascript 解决方案,它会工作吗?
【问题讨论】:
标签: php twitter google-plus