【问题标题】:get share count for google and twitter with php not working anymore使用 php 获取 google 和 twitter 的共享计数不再工作
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

    • 作者附加了错误消息,这不是问题的正确答案。应该是评论。
    猜你喜欢
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 2018-04-06
    • 2015-08-28
    • 2022-06-16
    相关资源
    最近更新 更多