Difficulty getting google plus one count

亡梦爱人 提交于 2019-12-01 11:24:45

Probably a problem with curl not accepting the CA of the server. You can find out for sure with:

$curl_results = curl_exec ($ch);
echo curl_error($ch);

If it is indeed a problem with the untrusted CA, you have two options. The insecure and easy way would be to add one more option to curl:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

This disables the check. The second option (better and a bit more complicated) would be to go to https://clients6.google.com and export the CA certificate and feed it to curl like so:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/exported.crt");

The cURL and API way listed in the other posts here no longer works.

There is still at least 1 method, but it's ugly and Google clearly doesn't support it. You just rip the variable out of the JavaScript source code for the official button with a regular expression:

function shinra_gplus_get_count( $url ) {
    $contents = file_get_contents( 
        'https://plusone.google.com/_/+1/fastbutton?url=' 
        . urlencode( $url ) 
    );

    preg_match( '/window\.__SSR = {c: ([\d]+)/', $contents, $matches );

    if( isset( $matches[0] ) ) 
        return (int) str_replace( 'window.__SSR = {c: ', '', $matches[0] );
    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!