Tumblr API 如何在 php 中访问以下数组中的各个值
我正在使用 tumblr API 和以下代码:
$var = xhttp::toQueryArray($response['body']);
print_r($var);
这会在屏幕上打印以下内容:
Array ( [{"meta":{"status":200,"msg":"OK"},"response":{"user":{"name":"lukebream","likes":0,"following":8,"default_post_format":"html","blogs":[{"name":"lukebream","url":"http:\/\/lukebream.tumblr.com\/","followers":5,"primary":true,"title":"Untitled","admin":true,"queue":0,"ask":false,"tweet":"N"}]}}}] => )
如何访问各个元素并将它们分配给变量?
这是我完成的内容:
$tumblr->set_token($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$data = array();
$data['post'] = array();
$response = $tumblr->fetch('http://api.tumblr.com/v2/user/info', $data);
if($response['successful']) {
echo $response['json']['response']['url'];
} else {
echo "api call failed. {$response[body]}<br><br>";
}
I'm using the tumblr API and the following code:
$var = xhttp::toQueryArray($response['body']);
print_r($var);
This print on the screen the following:
Array ( [{"meta":{"status":200,"msg":"OK"},"response":{"user":{"name":"lukebream","likes":0,"following":8,"default_post_format":"html","blogs":[{"name":"lukebream","url":"http:\/\/lukebream.tumblr.com\/","followers":5,"primary":true,"title":"Untitled","admin":true,"queue":0,"ask":false,"tweet":"N"}]}}}] => )
How can I access the individual elements and assign them to variables?
Here is what I have finished with:
$tumblr->set_token($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$data = array();
$data['post'] = array();
$response = $tumblr->fetch('http://api.tumblr.com/v2/user/info', $data);
if($response['successful']) {
echo $response['json']['response']['url'];
} else {
echo "api call failed. {$response[body]}<br><br>";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它称为 JSON,您可以使用
json_decode()
使用示例:
$jsonData
后面 包含 :经过
json_decode()
,我们得到一个 PHP 对象,因此 :将返回 :
然后您可以像访问任何其他对象一样访问数据。
It's called JSON, you can parse it using
json_decode()
Usage Example :
The $jsonData contains :
after it goes through
json_decode()
, we get a PHP object, so :will return :
Then you can access the data like with any other object.
您还可以使用 json_decode($str, TRUE):这将返回一个 ARRAY 而不是一个对象,更容易使用!
You can also use json_decode($str, TRUE): this will return an ARRAY instead of an object, much easier to play with!