php - 使用 curl 和 php 从 url 读取 xml 数据

标签 php xml json curl

我想从 URL 中读取 XML 数据。我的网址如下:

http://www.arrowcast.net/fids/mco/fids.asp?sort=city&city=&number=&airline=&adi=A

这是我的代码:

$Url="http://www.arrowcast.net/fids/mco/fids.asp?sort=city&city=&number=&airline=&adi=A";  
if (!function_exists('curl_init')){
    die('Sorry cURL is not installed!');
} 
   $ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $Url);   
$output = curl_exec($ch);
$resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close the cURL resource, and free system resources
curl_close($ch);

谁能告诉我如何读取 xml 数据?

最佳答案

这是一些示例代码(您的 PHP 安装可能不提供 XML 解析模块):

<?php

$url="http://www.arrowcast.net/fids/mco/fids.asp?sort=city&city=&number=&airline=&adi=A";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);    // get the url contents

$data = curl_exec($ch); // execute curl request
curl_close($ch);

$xml = simplexml_load_string($data);
print_r($xml);

?>

变量 $xml 现在是一个多维键值数组,您应该很容易就能弄清楚如何从那里获取元素。

关于php - 使用 curl 和 php 从 url 读取 xml 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11553678/

相关文章:

php paypal express结账问题

php - MySQLi 和 Phalanger

java - 如何在 Android 中使用 XPath 编辑属性

php - mysql表转json

json - 在 iex 中运行 elixir poison 命令

javascript - 如何将数据发送到jade(pug)中的JSON属性?

php - 使用新值更新联结表

php - 将 url 更改为 "seo-url"

xml - 使用 axios 向 SOAP 端点发出请求

javascript - 带有 javascript 的多功能 xml 属性正则表达式