get_permalink for json_decode无法正常工作

时间:2013-08-23 07:17:05

标签: php json wordpress function

这可能是一个直接的解决方案,但我真的无法让它发挥作用。我的代码如下:

PHP (functions.php)

<?php
class shareCount {
    private $url,$timeout;

    function __construct($url,$timeout=10) {
        $this->url=rawurlencode($url);
        $this->timeout=$timeout;
    }

    function get_tweets() { 
        $json_string = $this->file_get_contents_curl(
            'http://urls.api.twitter.com/1/urls/count.json?url=' . $this->url
        );
        $json = json_decode($json_string, true);
        return isset($json['count'])?intval($json['count']):0;
    }
}
?>

正如您所看到的,上面有两个函数:一个用于获取要解码的链接,另一个用于解码json信息并返回一个数字。

我在html中调用两个函数如下:

<div>

<?php
$obj=new shareCount("get_permalink()");
echo $obj->get_tweets();
?>

</div>

我遇到的问题是在html / php中,我调用函数,"get_permalink"将无法在""标记内工作。如果我删除引号它也不起作用。此设置似乎有效的唯一方法是将链接手动放在引号内。

我需要使用get_permalink()或类似内容来提取当前帖子网址,然后将json_decode功能添加到其中。

由于

2 个答案:

答案 0 :(得分:1)

对于有同样问题的人,我找到了解决这个问题的方法。花了一段时间,但我终于修复了它。最终代码如下:

PHP(functions.php)

<?php

    function get_tweets($url) {
        $url = get_permalink($post->ID)
        $json_string = $this->file_get_contents_curl(
            'http://urls.api.twitter.com/1/urls/count.json?url=' . $url
        );
        $json = json_decode($json_string, true);
        return isset($json['count'])?intval($json['count']):0;
    }
}
?>

并按如下方式调用HTML中的函数:

<div>

<?php
echo get_tweets($url);
?>

</div>

基本上你需要指定php函数“get_tweets”是一个url。然后将该url指定为“$ url = get_permalink($ post-&gt; ID)。然后当您在html中调用该函数时,只需编写echo get_tweets($ url)。

答案 1 :(得分:0)

$ this-&gt; file_get_contents_curl替换了file_get_contents

相关问题