SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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("\n");

    curl_close($ch);

    Thanks!

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    are you hosting this file on your own server, or with a company?

    Some hosting companies (GoDaddy notably) require you to use a proxy server with cURL.

  3. #3
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The file is provided to me by an outside server. As I mentioned before, if I copy the file onto my own host it works fine. How do I know if I need to use a proxy?

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    I mean the PHP file; your host may have proxy requirements. As i said, i know GoDaddy have proxy requirements to reach out to an external server.

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,152
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    I was unaware of proxy requirements on some hosts. That could very well be the problem.

    I've used cURL in a few scripts but I have never used CURLOPT_HEADER and I have always used CURLOPT_CONNECTTIMEOUT

    If you try this does it work?

    PHP Code:
    $ch curl_init();
    curl_setopt($chCURLOPT_URL,
    'http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/NS/s0000450_e.xml');
    curl_setopt($chCURLOPT_CONNECTTIMEOUT30);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    $xmlContents curl_exec($ch);

    print(
    "XML contents = ");
    print_r($xmlContents);
    print(
    "\n");

    curl_close($ch); 

  6. #6
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    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?

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    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?

  9. #9
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The file responsed fairly fast. You can try
    http://dd.weatheroffice.ec.gc.ca/cit...s0000450_e.xml

  10. #10
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  11. #11
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No luck with file_get_contents()

  12. #12
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,152
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    I just tried my above code and view-source shows
    Code XML:
    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 Code:
    <?php
    phpinfo
    ();
    ?>
    shows cURL?

  13. #13
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    if cURL wernt installed, wouldnt it be belching function_undefined errors?

  14. #14
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Now that, would depend on the OPs error reporting/settings.

    Normally, crmalibu would be the first to post the obligatory:-
    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);
    ...snippet - and rightly so.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  15. #15
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    What does var_dump(curl_getinfo($ch)) output?
    Salathe
    Software Developer and PHP Manual Author.

  16. #16
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Now that, would depend on the OPs error reporting/settings.

    Normally, crmalibu would be the first to post the obligatory:-
    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);
    ...snippet - and rightly so.
    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.

  17. #17
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 "\n 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("\n\n *********** displaying ch info ***************** \n");
    var_dump(curl_getinfo($ch));
    print("\n ************ End **************** \n\n");
    print("XML Contents = ");
    print_r($xmlContents);
    print("\n");
    curl_close($ch);
    ...
    Attached Files Attached Files

  18. #18
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    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.
    Salathe
    Software Developer and PHP Manual Author.

  19. #19
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  20. #20
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?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?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  21. #21
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  22. #22
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  23. #23
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank all for your help. The problem is solved. It is not proxy server, my host server black listed the targeted IP.

  24. #24
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,152
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Black listed a Canadian Weather service!

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •