使用 curl 和 php 从 url 读取 xml 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11553678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read xml data from url using curl and php
提问by user1403175
I want to read XML data from a URL. I have the url as follow
我想从 URL 读取 XML 数据。我的网址如下
http://www.arrowcast.net/fids/mco/fids.asp?sort=city&city=&number=&airline=&adi=A
http://www.arrowcast.net/fids/mco/fids.asp?sort=city&city=&number=&airline=&adi=A
Here is my code
这是我的代码
$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);
Could anyone please tell me how to read xml data?
谁能告诉我如何读取xml数据?
Thanks.....
谢谢.....
回答by Kartik
Here is some sample code (XML parsing module may not be available on your PHP installation):
下面是一些示例代码(XML 解析模块可能在您的 PHP 安装中不可用):
<?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)
?>
The variable $xmlnow is a multi-dimensional key value array and you should easily be able to figure out how to get the elements from there.
该变量$xml现在是一个多维键值数组,您应该很容易弄清楚如何从那里获取元素。
回答by sanna
try { URL url=new URL("http://durgajobs.com/Bangalore.html");
尝试 { URL url=new URL(" http://durgajobs.com/Bangalore.html");
URLConnection con=url.openConnection();
int x=0;
while(true)
{
x=con.getInputStream().read();
if(x==-1)
{
break;
}
System.out.print((char)x);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I need same code in php
我需要在 php 中使用相同的代码
回答by Arshad Shaikh
This is the demo of how to get channel id from rss feed of youtube in which i read xml data from url using curl.
这是如何从 youtube 的 rss 提要获取频道 ID 的演示,其中我使用 curl 从 url 读取 xml 数据。
$channel_id = 'XXXXXXXX'; // put the channel id here
//using curl
$url = 'https://www.youtube.com/feeds/videos.xml?channel_id='.$channel_id.'&orderby=published';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$response = curl_exec($ch);
curl_close($ch);
$response=simplexml_load_string($response);
$json = json_encode($response);
$youtube= json_decode($json, true);
$count = 0;
if(isset($youtube['entry']['0']) && $youtube['entry']['0']!=array())
{
foreach ($youtube['entry'] as $k => $v) {
$yt_vids[$count]['id'] = str_replace('http://www.youtube.com/watch?v=', '', $v['link']['@attributes']['href']);
$yt_vids[$count]['title'] = $v['title'];
$count++;
}
}
else
{
$yt_vids[$count]['id']=str_replace('http://www.youtube.com/watch?v=', '', $youtube['entry']['link']['@attributes']['href']);
$yt_vids[$count]['title']=$youtube['title'];
}
echo "<pre>";
print_r($yt_vids);
回答by Jon
try { URL url=new URL("https://www.freshers-job.com/");
URLConnection con=url.openConnection();
int x=0;
while(true)
{
x=con.getInputStream().read();
if(x==-1)
{
break;
}
System.out.print((char)x);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
回答by Hiren Soni
// the SAX way:
XMLReader myReader = XMLReaderFactory.createXMLReader();
myReader.setContentHandler(handler);
myReader.parse(new InputSource(new URL(url).openStream()));
// or if you prefer DOM:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new URL(url).openStream());

