XML而不是JSON [英] XML instead of json

查看:143
本文介绍了XML而不是JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个XML响应连接到一个API。我有它在JSON的工作(见下文),但我需要它在XML返回来代替。

I am trying to connect to an API with an XML response. I have it working in json (see below) but i need it to return in xml instead.

//check if you have curl loaded
if(!function_exists("curl_init")) die("cURL extension is not installed");

$url = 'http://api.klout.com/1/klout.xml?users=username&key=keyhere';

$ch=curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r=curl_exec($ch);
curl_close($ch);

$arr = json_decode($r,true);

foreach($arr['users'] as $val)
{
        echo $val['kscore'].'<br>';
        echo $val['twitter_screen_name'].'<br>';
}

感谢。

克里斯

推荐答案

您使用的是XML格式返回数据的API。所以,你不能使用 json_de code()来解析它,很明显。相反,你应该看的 的SimpleXML 。这是默认的PHP库来解析和编写XML数据,通常谈到在默认情况下安装PHP

The API you are using returns data in XML format. So, you cannot use json_decode() to parse it, obviously. Instead, you should look to SimpleXML. It's the default PHP library to parse and write XML data, and usually it comes installed by default with PHP.

您可以用这个漂亮的教程启动:

You can start with this nice tutorial:

http://www.phpro.org/tutorials/介绍到的SimpleXML与 - PHP.html

或者,如果你想开始非常非常快:

Or if you want to start very very quickly:

http://www.w3schools.com/php/php_xml_simplexml.asp

这很容易用它来解析XML数据。

It's very easy to use it to parse XML data.

这篇关于XML而不是JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆