简体   繁体   中英

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
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.

I call the two functions in my html as follows:

<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. 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.

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

    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:

<div>

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

</div>

Basically you need to specify that the php function "get_tweets" is a 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).

$ this-> file_get_contents_curl替换为file_get_contents

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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