1

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.

7
  • Is the PHP script running on the same machine as your browser? Just because the url works in one places means absolutely nothing about how well it'll run on another machine. Commented Jun 29, 2011 at 20:54
  • 3
    @Blueboye - In addition to @Marc B's comment, 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. Commented Jun 29, 2011 at 20:55
  • I am running the PHP on my localhost but the ULR is an external server. Commented Jun 29, 2011 at 20:56
  • @Francois: WOW! That worked, though now I see the XML nodes data on the screen. It is not XML. How can I make it show as an XML. Another question I have is what is the best way to work on such a XML response? Should I store it in an XML file and then perform operations or can I directly perform XML operations on the response? Commented Jun 29, 2011 at 21:00
  • Possible Duplicates: stackoverflow.com/questions/1115638/php-curl-not-returning-xml ; stackoverflow.com/questions/5405424/… Commented Jun 29, 2011 at 21:01

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Great. Thanks a lot for your quick help. Any thoughts about my second question: Do I need to save this output into XML file to read the XML nodes or can I do it without saving too?
Isn't it better refer to htmlspecialchars()? And actually the encoding should be reflected before outputting the XML as HTML with a totally different encoding.
@Blueboy, you can do it without saving. Use DomDocument, works with files and strings.
@Blueboye - If you want to read the XML in PHP, have a look at SimpleXML (like simplexml_load_string). There's no need to save. You'll essentially want to do $xml = simplexml_load_string(curl_exec($ch));. If you want to echo the entire XML to the screen (so you can see it), you'll want to use htmlentities() as I said in my answer because the browser will not show it properly otherwise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.