六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 449|回复: 0

PHP解析JSON与XML,可解析远程json

[复制链接]
 楼主| 发表于 2013-5-7 10:26:39 | 显示全部楼层 |阅读模式
PHP解析JSON与XML,可解析远程json
  1. $data = file_get_contents($url);//目的页面内容获取
  2. $t = json_decode($data,1);//转换为PHP数组
  3. //处理...
  4. $ch = curl_init();
  5. curl_setopt($ch, CURLOPT_URL, $urlo);//数据发送地址
  6. curl_setopt($ch, CURLOPT_POST, 1);
  7. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//发送的数据数组
  8. curl_exec($ch);
复制代码
与大多数流行的 Web 服务如 twitter 通过开放 API 来提供数据一样,它总是能够知道如何解析 API 数据的各种传送格式,包括 JSON,XML 等等。
  1. PHP解析JSON数据
  2. $json_string='{"id":1,"name":"foo","email":"foo@foobar.com","interest":["wordpress","php"]} ';
  3. $obj=json_decode($json_string);
  4. echo $obj->name; //prints foo
  5. echo $obj->interest[1]; //prints php

  6. PHP解析XML 数据
  7. $xml_string="<?xml version='1.0'?>
  8. <users>
  9. <user id='398'>
  10. <name>Foo</name>
  11. <email>foo@bar.com</name>
  12. </user>
  13. <user id='867'>
  14. <name>Foobar</name>
  15. <email>foobar@foo.com</name>
  16. </user>
  17. </users>";

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

  20. //loop through the each node of user
  21. foreach ($xml->user as $user)
  22. {
  23. //access attribute
  24. echo $user['id'], ' ';
  25. //subnodes are accessed by -> operator
  26. echo $user->name, ' ';
  27. echo $user->email, '<br />';
  28. }
复制代码
本文摘自:http://www.cnblogs.com/yilee/archive/2011/08/18/2145080.html
该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表