将PERMALINK存储在PHP变量中

19 php variables

我有这个代码来计算我帖子中所有社交网站的份额.代码的问题是永久链接是静态的.

如果你注意到,我将永久链接存储在$myurl变量中get_permalink();,代码的问题就是我改变了链接" http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-采访/ ",它给了我很多错误,如:

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>";


?>
Run Code Online (Sandbox Code Playgroud)

要添加,该get_permalink函数将返回活动页面的当前URL.我这样做是为了将它整合到我的wordpress网站并显示确切的股票数量(facebook,twitter,linkedin和pinterest).上面的代码不会返回当前页面的URL.这是静态的.

jer*_*ity 4

对我来说,这看起来像是一些简单的疏忽。

第一个是对$url不存在的变量进行编码。目前你有

$myurl = get_permalink();
$url = urlencode($url);
Run Code Online (Sandbox Code Playgroud)

...当它可能应该是

$myurl = get_permalink();
$url = urlencode($myurl);
Run Code Online (Sandbox Code Playgroud)

第二个是没有定义名为twitter_tweet_countfacebook_like_share_count或 的函数pinterest_pins。相反,您使用匿名函数将它们分配给变量来确定它们的值。

本质上你应该能够改变这个

echo twitter_tweet_count( 'http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/' );
Run Code Online (Sandbox Code Playgroud)

……对此

echo $twitter_tweet_count( 'http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/' );
Run Code Online (Sandbox Code Playgroud)

...唯一的区别是文本$之前twitter_tweet_count

一旦修复,您应该能够进一步结合这两个修复,最终产品看起来像这样(使用 的动态值$url):

<?php

$myurl = get_permalink();
$url = urlencode($myurl);

$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( $url );

echo "<br>";

echo $facebook_like_share_count( $url );

echo "<br>";

echo $pinterest_pins( $url );

echo "<br>";

?>
Run Code Online (Sandbox Code Playgroud)