Chinaunix首页 | 论坛 | 博客
  • 博客访问: 793595
  • 博文数量: 247
  • 博客积分: 166
  • 博客等级: 入伍新兵
  • 技术积分: 2199
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-15 16:10
文章分类

全部博文(247)

文章存档

2017年(1)

2015年(63)

2014年(80)

2013年(94)

2012年(9)

分类: PHP

2013-07-17 14:13:40

与大多数流行的 Web 服务如 twitter 通过开放 API 来提供数据一样,它总是能够知道如何解析 API 数据的各种传送格式,包括 JSON,XML 等等。

PHP解析JSON数据

$json_string='{"id":1,"name":"foo","email":"foo@foobar.com","interest":["wordpress","php"]} ';
$obj=json_decode($json_string);
echo $obj->name; //prints foo
echo $obj->interest[1]; //prints php

PHP解析XML 数据

$xml_string="xml version='1.0'?>


Foo
foo@bar.com


Foobar
foobar@foo.com

"; 

//load the xml string using simplexml
$xml = simplexml_load_string($xml_string); 

//loop through the each node of user
foreach ($xml->user as $user)
{
//access attribute
echo $user['id'], ' ';
//subnodes are accessed by -> operator
echo $user->name, ' ';
echo $user->email, '
';
}
阅读(412) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~