gpt4 book ai didi

php - 将 PERMALINK 存储在 PHP 变量中

转载 作者:IT王子 更新时间:2023-10-29 00:04:02 26 4
gpt4 key购买 nike

我有这段代码可以计算我帖子中所有社交网站的分享。代码的问题是永久链接是静态的。

如果您注意到,我使用 get_permalink(); 将永久链接存储在 $myurl 变量中,代码的问题是一旦我更改链接“http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/” ",它给了我很多错误,例如:

file_get_contents(http://graph.facebook.com/?id=$myurl): failed to open stream: HTTP request failed!

这是我的代码:

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


?>

要添加,get_permalink 函数返回事件页面的当前 URL。我这样做是为了将它集成到我的 wordpress 网站并显示确切的共享数量(facebook、twitter、linkedin 和 pinterest)。上面的代码不返回当前页面的 URL。它是静态的。

最佳答案

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

首先是 $url 正在编码一个不存在的变量。目前你有

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

... 应该是什么时候

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

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

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

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

...为此

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

...唯一的区别是 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>";

?>

关于php - 将 PERMALINK 存储在 PHP 变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29700063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com