【问题标题】:store the PERMALINK in a PHP variable将 PERMALINK 存储在 PHP 变量中
【发布时间】:2015-04-17 13:05:19
【问题描述】:

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

如果您注意到,我使用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): 失败 打开流: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>";


?>

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

【问题讨论】:

  • 查看这些,看来您需要在输入 $url 上调用 urlencode()。所以$url = urlencode($url);——因为它在发送到file_get_contents() 的API 调用中用作查询字符串参数。不过,这可能不是唯一导致问题的原因。
  • 关于样式的注释 - 您在此处的内容适用于大多数 PHP 版本,但似乎受到 JavaScript 函数定义样式的严重影响。这对于 PHP 来说是非常陌生的。函数定义会更传统如function pinterest_pins($url) {...},然后在不使用$ 的情况下调用pinterest_pins('http://...');,而不是将函数分配给变量并通过变量函数调用。
  • 这段代码的奇怪之处,如果我使用了 echo twitter_tweet_count("google.com");,echo facebook_like_share_count("google.com);,它工作得很好。如果我会显示错误将静态链接更改为 $myurl

标签: php variables


【解决方案1】:

在我看来,这只是一些简单的疏忽。

首先是$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>";

?>

【讨论】:

  • 我收到此错误:警告:file_get_contents(graph.facebook.com/…):打开流失败:HTTP 请求失败! HTTP/1.1 403 禁止
【解决方案2】:

阅读您在 cmets 中给出的最后一个错误代码:403(禁止访问)

所以我查看了 facebook API 文档并找到了这个:Link to docs

您请求的操作之一可能会引发错误。 这可能是因为,例如,您没有权限 执行请求的操作。

在此引用之后的示例中,我们可以看到请求的 403 响应代码。

好吧,我可以从我的导航器和 PHP file_get_contents 函数访问这个页面。所以也许你被列入黑名单,或者有一些配额......如果问题仍然存在,你应该联系 Facebook 了解为什么你的服务器无法访问这些信息。

{
   "id": "http://alphaomega.bendaggers.com/2014/10/25/legendary-moments-of-barney-stinson/",
   "shares": 4
}

希望对您有所帮助:)

【讨论】:

    【解决方案3】:

    有两种方法可以避免此错误。

    第一种方法

    如果您要打开带有特殊字符(例如空格)的 URI,则需要使用 urlencode() 对 URI 进行编码。

    urlencode - This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.
    

    查看下面的代码

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

    第二种方法

    file_get_contents() 使用 fopen() 包装器,因此它被限制通过 php.ini 中的 allow_url_fopen 选项访问 URL。

    Should I allow 'allow_url_fopen' in PHP?

    第三种方法

    你可以试试 curl。试试这个link

    【讨论】:

      猜你喜欢
      • 2021-06-22
      • 2014-03-24
      • 2017-07-11
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 2011-07-15
      • 2016-05-01
      • 2013-10-11
      相关资源
      最近更新 更多