get_permalink for json_decode not working

2019-06-08 08:49发布

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

2条回答
Anthone
2楼-- · 2019-06-08 09:10

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

查看更多
该账号已被封号
3楼-- · 2019-06-08 09:19

$this->file_get_contents_curl replaced file_get_contents

查看更多
登录 后发表回答