我想知道如何将 json 字符串放入 php 中。这是我的字符串:
你应该看到这个返回:
twttr.receiveCount({"count":0,"url":"http:\/\/www.onewiththem.com.au\/"});
我想知道如何获取计数并将其设置在变量 $count 中?
我想知道如何将 json 字符串放入 php 中。这是我的字符串:
你应该看到这个返回:
twttr.receiveCount({"count":0,"url":"http:\/\/www.onewiththem.com.au\/"});
我想知道如何获取计数并将其设置在变量 $count 中?
简单,使用json_decode()
and file_get_contents()
:
$data = json_decode(file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=http://www.onewiththem.com.au/'));
echo $data->count;
请注意,我&callback=
从 URL 中删除了 ,因为它仅用于 JSONP 而 PHP 不需要它。
如果您的意思是在 javascript 中获取它,那么data
在您的回调函数中就是对象。你可以通过 得到计数data.count
。
twttr.reciveCount = function (data) {
console.log(data.count);
// do the rest
}
如果从 php 调用 api,则不应使用该callback
参数。获取 JSON 响应,然后用于json_decode
对其进行解码。(不要忘记对 url 参数进行 urlencode。)
$response = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url='.urlencode('http://www.onewiththem.com.au/'));
$json = json_decode($response);
echo $json->count;