获取谷歌和推特的分享数量与PHP不再工作

时间:2015-01-14 21:07:47

标签: php twitter google-plus

我用这些函数来获取社交份额:

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和谷歌,我收到了这个错误:

failed to open stream: Connection timed out

我认为我的IP已被谷歌和Twitter禁止,但我不确定。 有人可以证实我的假设吗?

如果我通过javascript解决方案,它会起作用吗?

1 个答案:

答案 0 :(得分:-1)

php_curl手册:http://php.net/manual/en/function.curl-getinfo.php

CURLINFO_HTTP_CODE将告诉您API服务器发送的HTTP响应代码。如果您正在测试环境,请尝试将其记录或打印在页面上。如果响应代码类似于403,则API服务器已拒绝从您的服务器发送的请求。