Reputation: 3671
I've got a script that parses an XML file and saves it to a database. I'm attempting to grab the XML file from an external URL but it doesn't work. It does, however, work when I test the script locally. For example, I go to the URL I'm trying to parse, save that file to my computer, upload it to my server and use this script:
$url = 'sample_xml/sample.xml';
$xml = simplexml_load_file($url);
It works fine. When I then try to run the same script but I substitute the actually url into the $url variable, I get this error:
Warning: simplexml_load_file(): Couldn't resolve host name in /foo/foo.php on line 12
Is it possible that the server I'm trying to parse from won't allow it? I have no problem pulling the XML file up in a browser window and it's not a password protected site or anything so I'm wondering why simplexml_load_file isn't able to resolve the host name.
Thanks for your help!
Upvotes: 1
Views: 627
Reputation: 1642
1) Try to load it with or without "www."
2) Have You tried to load it as described on the PHP web site simplexml_load_file (I mean using the file_exists()
)?:
<?php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.
if (file_exists('test.xml')) {
$xml = simplexml_load_file('test.xml');
print_r($xml);
} else {
exit('Failed to open test.xml.');
}
?>
3) It is stated, that PHP version should be not less than version 5.1.0 (otherwise You need to use urlencode()
)
4) Does Your URL contains any non-latin characters?
5) On some hosts url access can depend on User-Agent string that requested it.
<?php
$url = "http://example.com/"; // replace with your url
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
// the line below makes the common user agent to be present in the request:
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$data = curl_exec($ch);
curl_close($ch);
echo $data;
?>
6) Make sure that the IP from which you run the script is not blacklisted
Upvotes: 2