无法通过cURL获取XML输出 [英] Cannot get XML output through cURL

查看:161
本文介绍了无法通过cURL获取XML输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP cURL从网址获取XML输出。这是我的代码看起来像:

  $ ch = curl_init 
curl_setopt($ ch,CURLOPT_URL,'http://www.mydomain.com?querystring');
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($ ch,CURLOPT_USERPWD,username:password);
$ store = curl_exec($ ch);
echo $ store;
curl_close($ ch);

但是,不是返回XML,而是显示我的404错误页面。如果我在网络浏览器中键入网址 http://www.mydomain.com?querystring ,我可以看到浏览器中的XML。



我在这里缺少什么? :

解决方案

部分网站拥有者检查存在某些事情以确保请求来自一个Web浏览器而不是一个bot(或cURL)。你应该尝试添加 curl_setopt($ ch,CURLOPT_USERAGENT,'Mozilla / 5.0(compatible; MSIE 9.0; Windows NT 6.1; Trident / 5.0)'); ,看看是否能解决这个问题,这将发送一个user-agent字符串,网站也可以检查是否存在cookie或其他东西。 / p>

要在网页中输出XML,您需要使用< pre> 元素。


I am using PHP cURL to fetch XML output from a URL. Here is what my code looks like:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://www.mydomain.com?querystring'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
$store = curl_exec($ch);
echo $store;
curl_close($ch);

But, instead of returning the XML it just shows my 404 error page. If I type the URL http://www.mydomain.com?querystring in the web browser I can see the XML in the browser.

What am I missing here? :(

Thanks.

解决方案

Some website owners check for the existence of certain things to make sure the request comes from a web browser and not a bot (or cURL). You should try adding curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)'); and see if that fixes the problem. That will send a user-agent string. The site may also check for the existence of cookies or other things.

To output the XML in a web-page, you'll need to use htmlentities(). You might want to wrap it inside a HTML <pre> element as well.

这篇关于无法通过cURL获取XML输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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