【问题标题】:Share Counter only works on one post?分享计数器只适用于一个帖子?
【发布时间】:2014-08-04 20:19:54
【问题描述】:

我有一些代码只适用于一篇文章,但我希望它适用于所有文章,而不仅仅是列出的文章我收到以下错误:

(致命错误:无法在第 3 行的 counter/share-count.php 中重新声明类 shareCount)

loop.php(这是我每个帖子的循环代码)

<?
require("counter/share-count.php");
$obj=new shareCount("http://google.com");
echo "Tweets: ".$obj->get_tweets();
echo "<br>Facebook: ".$obj->get_fb(); 
echo "<br>Google+: ".$obj->get_plusones();
?>

share-count.php(这是可根据要求执行的文件)

<?
class shareCount {
private $url,$timeout;
function __construct($url,$timeout=10) {
$this->url=rawurlencode($url);
$this->timeout=$timeout;
}

function get_tweets() { 
$json_string = $this->file_get_contents_curl('http://urls.api.twitter.com/1/urls/count.json?url=' . $this->url);
$json = json_decode($json_string, true);
return isset($json['count'])?intval($json['count']):0;
}

function get_fb() {
$json_string = $this->file_get_contents_curl('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls='.$this->url);
$json = json_decode($json_string, true);
return isset($json[0]['total_count'])?intval($json[0]['total_count']):0;
}

function get_plusones()  {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'.rawurldecode($this->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);
return isset($json[0]['result']['metadata']['globalCounts']['count'])?intval( $json[0]['result']['metadata']['globalCounts']['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_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
$cont = curl_exec($ch);
if(curl_error($ch))
{
die(curl_error($ch));
}
return $cont;
}
}
?>

【问题讨论】:

  • 请格式化您的代码!

标签: php wordpress web share posts


【解决方案1】:

您包含的文件 counter/share-count.php 每次包含时都会定义 shareCount 类,这就是您看到错误的原因。您可以通过使用require_once() 来解决此问题,或者检查该类是否已使用class_exists() 定义,并且仅在结果为false 时才定义它。

require_once()代替require()

require_once("counter/share-count.php");
// ... rest of your code

使用class_exists()

if ( ! class_exists( 'shareCount' ) ):
class shareCount{
    // your class implementation
}
endif; // class_exists

【讨论】:

  • 请看我其他的答案,我正在尝试让 URL 为每个帖子工作
  • 这一切正常,但是因为我必须从网站中的另一个文档中调用它,它会使网站崩溃,有没有办法在网页本身中调用它,而不是从不同的文档中调用它?
猜你喜欢
  • 2017-01-28
  • 2018-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-15
  • 2016-01-02
  • 1970-01-01
相关资源
最近更新 更多