以编程方式获取LinkedIn页面的共享状态


Getting LinkedIn Share Stats Of A Page, programmatically

我正在寻找一种方法来获得linkedin共享按钮的页面状态。例如,我正在编写一个PHP类,当你在你的网站上使用我的类时,像这样:

include("shareButtonStats.class.php");
$stats = new shareButtonStats;
echo "This page shared " . $stats->show . " times on LinkedIn";

正如你所看到的,我需要得到这个页面的统计数据为多少次共享。

你知道当我们添加LİnkedIn的js文件分享按钮(从这里),它包括一些HTML代码到我们的页面。像这样:

<span class="IN-widget" style="line-height: 1; vertical-align: baseline; display: inline-block; text-align: center;">
<span style="padding: 0pt ! important; margin: 0pt ! important; text-indent: 0pt ! important; display: inline-block ! important; vertical-align: baseline ! important; font-size: 1px ! important;">
<span id="li_ui_li_gen_1321370527058_1-container" class="IN-top">
<span id="li_ui_li_gen_1321370527058_1" class="IN-top">   
 .......

如果我得到的HTML代码,不包括它的页面直接,我可以使用RegEx在它,然后得到共享按钮计数:)

你有什么主意吗?

我相信他们有,这是一个简单的网页,需要URL。它返回JavaScript或PHP都能解析的JSON

希望对您有所帮助

http://www.linkedin.com/countserv/count/share?url=http://www.apple.com

LinkedIn提供了一个外部API。你可以这样使用它:

function getLinkedInCount($url)
{
    $curl = curl_init("http://www.linkedin.com/cws/share-count?url=" . $url);
    if (is_resource($curl) === true)
    {
        curl_setopt($curl, CURLOPT_FAILONERROR, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_REFERER, "http://www.linkedin.com/");
        $result = false;
                $retries = 3;
        while (($result === false) && (--$retries > 0))
        {
            $result = curl_exec($curl);
        }
        curl_close($curl);
    }
    $arr = json_decode(substr($result, 26));
    return $arr->count;
}
echo getLinkedInCount("http://www.google.com/") . "'n";

我使用cURL来下载这个页面,但是你可以使用另一种方法来下载。

有关实时演示,请参见:http://codepad.viper-7.com/VDD5aI