繁体   English   中英

json_decode的get_permalink无法正常工作

[英]get_permalink for json_decode not working

这可能是一个简单的解决方法,但我真的无法使其正常工作。 我的代码如下:

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函数。

谢谢

对于有相同问题的任何人,我都找到了解决此问题的方法。 花了一段时间,但我终于解决了。 最终代码如下:

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-> ID)。然后,当您在html中调用该函数时,只需编写echo get_tweets($ url)。

$ this-> file_get_contents_curl替换为file_get_contents

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM