获取社交分享总数(Facebook、Twitter、Google+。Pinterest)

时间:2015-05-24 作者:japanworm

我在我的主题函数中使用以下代码片段来计算我提到的社交媒体的个人份额。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: 经过大量测试后,似乎是这段代码(回显时)降低了页面速度<耸耸肩我想我必须停止使用它了?

我还没能让它工作。我希望你能在这里帮助我。非常感谢你!

1 个回复
SO网友:Christine Cooper

好的@ialocin记录了关于Transients. 出于某种奇怪的原因,我还没有遇到这个问题。以前,我使用以下方式显示计数:

jQuery(客户端加载)

  • 页面缓存(在本地存储计数时)
  • Cron更新计数值,最近,将所有计数存储在post meta中,并每加载100次页面更新一次meta。。。非常疯狂

    // Check for transient. If none, then execute code
    if ( false === ( $data = get_transient( \'trans_\' . $post_id ) ) ) {
    
        /* action */
        $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\'] );
        };
    
        // store data in array
        $data = array (
            $facebook_like_share_count,
            $twitter_tweet_count,
            $pinterest_pins,
            $google_plusones
        );
    
        // Put the results in a transient. Expire after 6 hours
        set_transient( \'trans_\' . $post_id, $data, 6 * HOUR_IN_SECONDS  );
    }
    
    if (is_array($data)) {
    
        $facebook_like_share_count = $data[0];
        $twitter_tweet_count = $data[1];
        $pinterest_pins = $data[2];
        $google_plusones = $data[3];
    
    }
    
    首先,确保$post_id 之前已设置为添加此代码,因为它使用ID向瞬态添加唯一句柄。

    我们将缓存数据临时存储在数据库中,方法是为其指定一个自定义名称和一个时间段,在该时间段之后,缓存数据将过期并被删除。

    因此,它每6小时更新一次缓存的数组。就这么简单。请参阅评论以了解更多信息。

    编辑:

    根据评论线程中的讨论,我已经调整了代码。首先,确保您的functions.php 文件(我相信您当前的设置中已经有了此文件),然后在single.php 文件中,添加以下内容get 计数值。

    // get post id
    $post_id = get_the_ID();
    
    // get perm url to be used for share count functions
    $url = get_permalink( $post_id );
    
    // Check for transient. If none, then execute code
    if ( false === ( $data = get_transient( \'trans_\' . $post_id ) ) ) {
    
        /* action */
        $facebook_like_share_count ("$url");
        $twitter_tweet_count ("$url");
        $pinterest_pins ("$url");
        $google_plusones ("$url");
    
        // store data in array
        $data = array (
            $facebook_like_share_count,
            $twitter_tweet_count,
            $pinterest_pins,
            $google_plusones
        );
    
        // Put the results in a transient. Expire after 6 hours
        set_transient( \'trans_\' . $post_id, $data, 6 * HOUR_IN_SECONDS  );
    }
    
    if (is_array($data)) {
    
        // these are your variables containing the share count
        $facebook_like_share_count = $data[0];
        $twitter_tweet_count = $data[1];
        $pinterest_pins = $data[2];
        $google_plusones = $data[3];
    
    }
    
    请阅读我在代码中的注释以进行澄清。

  • 结束

    相关推荐