如何使用php访问Json字段 [英] How to access Json fields by using php
本文介绍了如何使用php访问Json字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在使用wordpress中的php访问json.我已经成功解码了json,但是当我尝试访问值时却无法获取.我正在尝试访问Cluster_ID和Image,Title值.这是我的api链接
我尝试了以下代码
<?php/***插件名称:SEC API**/函数myjson8(){$ request = wp_remote_get('http://ec2-13-127-149-66.ap-south-1.compute.amazonaws.com:5000/api/news');if(is_wp_error($ request)){返回false;//提早保释}$ body = wp_remote_retrieve_body($ request);$ data = json_decode($ body);if(!empty($ data)){foreach($ data-> NewsFeed-> data as $ new){回声$ new-> Cluster_ID;foreach($ data-> NewsFeed-> data-> data as $ old){回声$ old->图片;echo $ old-> Title;}}}}
解决方案
NewsFeed
的类型为数组,因此应为 $ data-> NewsFeed [0]-> data
I am currently working with Accessing json using php in wordpress. I have successfully decoded the json but when i try to access the values it doesn't fetch . I am trying to access the Cluster_ID and Image,Title values. Here's my api link http://ec2-13-127-149-66.ap-south-1.compute.amazonaws.com:5000/api/news
I have tried the following code
<?php
/**
*Plugin Name: SEC API
**/
function myjson8(){
$request = wp_remote_get( 'http://ec2-13-127-149-66.ap-south-1.compute.amazonaws.com:5000/api/news' );
if( is_wp_error( $request ) ) {
return false; // Bail early
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
if( ! empty( $data) ) {
foreach($data->NewsFeed->data as $new){
echo $new->Cluster_ID;
foreach($data->NewsFeed->data->data as $old){
echo $old->Image;
echo $old->Title;
}
}
}
}
解决方案
The type of NewsFeed
is array so It should be $data->NewsFeed[0]->data
这篇关于如何使用php访问Json字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文