将永久链接存储在 PHP 变量中
我有这个代码来计算我帖子中的所有社交网站共享。代码的问题是永久链接是静态的。
如果你注意到,我使用变量存储了永久链接,代码的问题是一旦我更改链接“http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/”,它就会给我大量的错误,例如:$myurl
get_permalink();
file_get_contents(http://graph.facebook.com/?id=$myurl):打开流失败:HTTP请求失败!
这是我的代码:
$myurl = get_permalink();
$url = urlencode($url);
$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;
};
echo twitter_tweet_count( 'http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/' );
echo "<br>";
echo facebook_like_share_count( 'http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/' );
echo "<br>";
echo pinterest_pins( 'http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/' );
echo "<br>";
?>
若要添加,该函数返回活动页的当前 URL。我这样做是为了将其集成到我的wordpress网站上,并显示确切的股票数量(facebook,twitter,LinkedIn和pinterest)。上面的代码不返回当前页面的 URL。它是静态的。get_permalink