gpt4 book ai didi

php - 在 PHP 中获取给定 URL 的 google plus 共享

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:06:09 26 4
gpt4 key购买 nike

我想获取 PHP 中给定 URL 在 google plus 上的分享数。我发现这个函数可以做到这一点:

function get_shares_google_plus($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$curl_results = curl_exec ($curl);
curl_close ($curl);
$json = json_decode($curl_results, true);
print_r($json);
return intval( $json[0]['result']['metadata']['globalCounts']['count'] );
}

但是,我总是收到相同的消息:注意: undefined index :导致...

我制作 print_r($json),我得到:Array ( [0] => Array ( [error] => Array ( [code] => 400 [message] => 无效值 [data] => Array ( [0] => Array ( [domain] => global [reason] => invalid [message] => Invalid Value ) ) ) [id] => p ).

有什么建议吗?

最佳答案

RPC API 从未打算供公众使用,Google 更改了身份验证以防止滥用。因此,您发布的代码不再有效。但是,我找到了一个更简单的解决方案:

更新(2013 年 1 月 23 日):Google 于 2012 年 12 月屏蔽了此网址 - 因此此方法不再有效!
更新 (15.05.2013): 该方法再次有效!

<?php
/**
* Get the numeric, total count of +1s from Google+ users for a given URL.
* @author Stephan Schmitz <eyecatchup@gmail.com>
* @copyright Copyright (c) 2013 Stephan Schmitz
* @license http://eyecatchup.mit-license.org/ MIT License
* @link <a href="https://gist.github.com/eyecatchup/8495140">Source</a>.
* @param $url string The URL to check the +1 count for.
* @return intval The total count of +1s.
*/
function getGplusShares($url) {
$url = sprintf('https://plusone.google.com/u/0/_/+1/fastbutton?url=%s', urlencode($url));
preg_match_all('/{c: (.*?),/', file_get_contents($url), $match, PREG_SET_ORDER);
return (1 === sizeof($match) && 2 === sizeof($match[0])) ? intval($match[0][1]) : 0;
}

更新 (18.01.2014):这是一个使用 curl 的改进版本,一个回退主机并进行了一些错误处理(最新版本可以在这里找到 https://gist.github.com/eyecatchup/8495140 )。

<?php
/**
* GetPlusOnesByURL()
*
* Get the numeric, total count of +1s from Google+ users for a given URL.
*
* Example usage:
* <code>
* $url = 'http://www.facebook.com/';
* printf("The URL '%s' received %s +1s from Google+ users.", $url, GetPlusOnesByURL($url));
* </code>
*
* @author Stephan Schmitz <eyecatchup@gmail.com>
* @copyright Copyright (c) 2014 Stephan Schmitz
* @license http://eyecatchup.mit-license.org/ MIT License
* @link <a href="https://gist.github.com/eyecatchup/8495140">Source</a>.
* @link <a href="http://stackoverflow.com/a/13385591/624466">Read more</a>.
*
* @param $url string The URL to check the +1 count for.
* @return intval The total count of +1s.
*/
function GetPlusOnesByURL($url) {
!$url && die('No URL, no results. ;)');

!filter_var($url, FILTER_VALIDATE_URL) &&
die(sprintf('PHP said, "%s" is not a valid URL.', $url));

foreach (array('apis', 'plusone') as $host) {
$ch = curl_init(sprintf('https://%s.google.com/u/0/_/+1/fastbutton?url=%s',
$host, urlencode($url)));
curl_setopt_array($ch, array(
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64) ' .
'AppleWebKit/537.36 (KHTML, like Gecko) ' .
'Chrome/32.0.1700.72 Safari/537.36' ));
$response = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);

if (200 === $curlinfo['http_code'] && 0 < strlen($response)) { break 1; }
$response = 0;
}
!$response && die("Requests to Google's server fail..?!");

preg_match_all('/window\.__SSR\s\=\s\{c:\s(\d+?)\./', $response, $match, PREG_SET_ORDER);
return (1 === sizeof($match) && 2 === sizeof($match[0])) ? intval($match[0][1]) : 0;
}

更新 (02.11.2017):+1 计数正式失效!正如在 this Google+ Post 中宣布的那样由产品经理 John Nack 撰写 Google 最近从他们的网络共享按钮中删除了共享计数(又名 +1 计数)。 (他们声称此举的目的是让 +1 按钮和分享框加载得更快。)

关于php - 在 PHP 中获取给定 URL 的 google plus 共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12391779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com