PHP to fetch XML file into a string from external server

Hi,

Wonder if someone can help. I am using the cURL function to fetch the XML file into a string from an external server. But I am not getting anything . If I took this ‘s0000450_e.xml’ file and stored it on my host server, the content is returned. My code is as follows…

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/NS/s0000450_e.xml’);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xmlContents = curl_exec($ch);

print("XML contents = “);
print_r($xmlContents);
print(”
");

curl_close($ch);

Thanks!

Black listed a Canadian Weather service!

I must admit that’s the last thing I would have thought of.

Thank all for your help. The problem is solved. It is not proxy server, my host server black listed the targeted IP.

The output is

text/x-generic AAA_20100616.dat
UTF-8 Unicode text, with CRLF, LF line terminators

X-Powered-By: PHP/5.2.13
Content-type: text/html


Error: couldn’t connect to host

Yeah, but if it was throwing a fatal (function undefined) error without error reporting, he’d get a BLANK screen, and his string echos wouldnt appear either, not to mention that he wouldnt have been able to cURL the local file.

Still, error reporting cant hurt.

if cURL wernt installed, wouldnt it be belching function_undefined errors?

phpinfo() shows that cURL is enabled as seen here
cURL support enabled
cURL Information libcurl/7.20.0 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5

Here is my a snippet of my code and var_dump output is in the attached text file. Can’t make out what the output mean…

$pageurl = “http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/” . $province . “/” . $data_file;
print "
pageurl = ".$pageurl;

// Use cURL to fetch XML content into a PHP string variable.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $pageurl);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xmlContents = curl_exec($ch);

//$xmlContents = file_get_contents(‘http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/NS/s0000450_e.xml’);

print("

*********** displaying ch info *****************
“);
var_dump(curl_getinfo($ch));
print(”
************ End ****************

");
print("XML Contents = “);
print_r($xmlContents);
print(”
");
curl_close($ch);

Now that, would depend on the OPs error reporting/settings.

Normally, crmalibu would be the first to post the obligatory:-


<?php
error_reporting(-1);
ini_set('display_errors', true);

…snippet - and rightly so.

Here you go…http://pastie.org/1006940

<?php
error_reporting(-1);
ini_set('display_errors', true);

if(false === function_exists('curl_init')){
  echo 'Error: curl extension not enabled/available';
  exit;
}

$curl = curl_init('http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/NS/s0000450_e.xml');
curl_setopt_array(
  $curl,
  array(
    CURLOPT_RETURNTRANSFER  => true
  )
);

$xml = curl_exec($curl);

if(false === $xml){
  echo 'Error: ' . curl_error($curl);
  exit;
}

echo $xml;
?>

…and the output of this is?

khuant, could you add the output/code to a pastebin site (like pastie), the attachment needs to be manually approved so we can’t see it yet.

I just tried my above code and view-source shows

XML contents = <?xml version='1.0' encoding='ISO-8859-1'?>
 <siteData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://dd.weatheroffice.gc.ca/citypage_weather/schema/site.xsd">
 <license>http://dd.weatheroffice.gc.ca/doc/LICENCE_GENERAL.txt</license>
 <dateTime name="xmlCreation" zone="UTC" UTCOffset="0">
 <year>2010</year>
 <month name="June">06</month>
 <day name="Wednesday">16</day>

 <hour>04</hour>
 <minute>52</minute>
 <timeStamp>20100616045200</timeStamp>
 <textSummary>Wednesday June 16, 2010 at 04:52 UTC</textSummary>
......

If you don’t have allow_url_fopen, are you sure you have cURL? i.e.

<?php
phpinfo();
?>

shows cURL?

No luck with file_get_contents()

What does var_dump(curl_getinfo($ch)) output?

I have not try file_get_content as I am not sure if their PHP is setup with allow_url_fopen set to true. But I will give it a try now.

Ah, so it looks like we maybe dealing with a proxy after all.

There are a few mentions of which servers to use on the interwebz, along with some accompanying code.

Raise a support ticket with GoDaddy or try a few of the solutions mentioned in the search above.

Good luck!

Anthony.

The file responsed fairly fast. You can try
http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/NS/s0000450_e.xml

Interestingly, I just looked godaddy’s policy on cURL up, and they removed the proxy requirement at the end of 2008 (but they’ve still got the servers running to accomodate legacy code)

khaunt, have you tried a standard file_get_contents on the URL?

Hmm… that is odd, as it should work the same… I suppose it’s possible that the server rejects connections that dont have a valid Agent code (Which is another CurlOpt), but i doubt it…

Is the server in question responding at a decent speed if you go to the file yourself? Is it slow?

Mittineague, I tried your code it didn’t work either. The content is blank.

Sorry Starlion I misunderstood your question. I don’t think my host uses a proxy as I was able to run the same code with another xml file from other server.

Any other suggestion?