我在我的主题函数中使用以下代码片段来计算我提到的社交媒体的个人份额。php:
$facebook_like_share_count = function ( $url ) {
$api = file_get_contents( \'http://graph.facebook.com/?id=\' . $url );
$count = json_decode( $api );
return $count->shares;
};
$twitter_tweet_count = function ( $url ) {
$api = file_get_contents( \'https://cdn.api.twitter.com/1/urls/count.json?url=\' . $url );
$count = json_decode( $api );
return $count->count;
};
$pinterest_pins = function ( $url ) {
$api = file_get_contents( \'http://api.pinterest.com/v1/urls/count.json?callback%20&url=\' . $url );
$body = preg_replace( \'/^receiveCount\\((.*)\\)$/\', \'\\\\1\', $api );
$count = json_decode( $body );
return $count->count;
};
$google_plusones = function ( $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 );
return intval( $json[0][\'result\'][\'metadata\'][\'globalCounts\'][\'count\'] );
};
我要把他们叫进我的单曲。php,代码如下:
<?php $url = get_permalink( $post_id ); echo $facebook_like_share_count ("$url");?>
<?php $url = get_permalink( $post_id ); echo $twitter_tweet_count ("$url");?>
<?php $url = get_permalink( $post_id ); echo $pinterest_pins ("$url");?>
<?php $url = get_permalink( $post_id ); echo $google_plusones ("$url");?>
这很好用。
现在,我试图找到一个代码片段,它将添加这4个服务的共享计数,并显示总的共享计数-可能类似于this here.
EDIT: 我有一个重要的问题。
Is it possible that the code above is slowing down my blog?
我已经联系了我的托管服务,他们告诉我它一定是插件或php文件之类的东西。我最近没有真正更新任何插件,P3 Profilier只是告诉我谁是常见的罪魁祸首。但在我的单曲中调用该函数后,我立即注意到了这一点。php my server有时加载速度非常慢,甚至在速度检查网站上超时。有什么想法吗?
EDIT2: 经过大量测试后,似乎是这段代码(回显时)降低了页面速度<耸耸肩我想我必须停止使用它了?
我还没能让它工作。我希望你能在这里帮助我。非常感谢你!