簡體   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