简体   繁体   English

json_decode的get_permalink无法正常工作

[英]get_permalink for json_decode not working

This is probably a straight forward fix, but I really can't get it to work. 这可能是一个简单的解决方法,但我真的无法使其正常工作。 My code reads as follows: 我的代码如下:

PHP (functions.php) 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;
    }
}
?>

As you can see there are two functions above: one which gets the link to be decoded and another to decode the json information and return a digit. 如您所见,上面有两个函数:一个用于获取要解码的链接,另一个用于对json信息进行解码并返回一个数字。

I call the two functions in my html as follows: 我在html中调用这两个函数,如下所示:

<div>

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

</div>

The issue I have is that in the html/php, where i call the functions, "get_permalink" will not work inside the the "" marks. 我的问题是在html / php中,我在其中调用函数的地方, "get_permalink"""标记内不起作用。 If i remove the quotation marks it also doesn't work. 如果我删除引号,它也不起作用。 The only way this setup seems to work is if a link is manually placed inside the quotes. 此设置似乎起作用的唯一方法是在引号内手动放置一个链接。

I need to use get_permalink() or something similar to pull the current post url and then add the json_decode function to it. 我需要使用get_permalink()或类似的方法来提取当前的发布网址,然后向其添加json_decode函数。

Thanks 谢谢

For anyone with the same issue , I have found the solution to this problem. 对于有相同问题的任何人,我都找到了解决此问题的方法。 It took a while but i finally fixed it. 花了一段时间,但我终于解决了。 The final code is as follows: 最终代码如下:

PHP (functions.php) 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;
    }
}
?>

And call the function in HTML as follows: 并按以下方式在HTML中调用该函数:

<div>

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

</div>

Basically you need to specify that the php function "get_tweets" is a url. 基本上,您需要指定php函数“ get_tweets”是一个url。 Then specifiy that url as "$url = get_permalink($post->ID). Then when you call the function in html just write echo 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