Home > Backend Development > PHP Tutorial > curl获取xml数据的问题

curl获取xml数据的问题

WBOY
Release: 2016-06-23 13:43:30
Original
1612 people have browsed it

我用curl获取xml文件链接,得到的只有内容,而没有结构。

比如:123red

得到的就是:123 red  

请问是需要设置哪里吗?


回复讨论(解决方案)

贴出代码看看。

function curlxml($url){    $header[] = "Content-type: text/xml";//定义content-type为xml    $ch = curl_init();      curl_setopt($ch, CURLOPT_URL, $url);      curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回        $r = curl_exec($ch);      curl_close($ch);             return $r;        }     $url="https://www.123456.com";  $value=curlxml($url); echo $value;  
Copy after login


因为链接都是临时生成的,所以没办法拿上来测试,请帮我看看是不是还要设置哪里,谢谢!

你只是获取到了值并没有对生成的XML进行解析,所以肯定不会得到你要的效果。

在输出前加个header。

function curlxml($url){    $header[] = "Content-type: text/xml";//定义content-type为xml    $ch = curl_init();      curl_setopt($ch, CURLOPT_URL, $url);      curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回        $r = curl_exec($ch);      curl_close($ch);              return $r;        }      $url="https://www.123456.com";   $value=curlxml($url); header('content-type:text/xml;charset=utf-8'); echo $value;  
Copy after login

谢谢楼上的,可能我没表达清楚,我是想让$r获得整个XML文件包括结构,我自己解决了,谢谢了

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template