SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    Community Advisor ULTiMATE's Avatar
    Join Date
    Aug 2003
    Location
    Bristol, United Kingdom
    Posts
    2,158
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)

    Failed Connection to RSS Feed using SimpleXML

    I am running some code which calls simplexml_load_file() to parse a XML file and output its details. The function works perfectly on my XAMPP setup, but on my work server I get this error:

    Warning: simplexml_load_file(http://www.live.com/news/results.asp...BNB&format=rss) [function.simplexml-load-file]: failed to open stream: Connection refused in rss.php on line 5

    Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://www.live.com/news/results.aspx?q=New+York&go=&form=QBNB&format=rss" in rss.php on line 5

    Notice: Trying to get property of non-object in rss.php on line 9

    Notice: Trying to get property of non-object in rss.php on line 9

    Warning: Invalid argument supplied for foreach() in rss.php on line 9
    Does anyone have any idea what the problem could be? I've uploaded a phpinfo() file and it seems that SimpleXML is loaded on there.

    Thank you!

  2. #2
    SitePoint Enthusiast Dharma's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The "Connection refused" error suggests that there is something going wrong on the other end - you said you can connect manually so that's obviously not it. Are you perhaps connecting on the wrong port?

  3. #3
    Community Advisor ULTiMATE's Avatar
    Join Date
    Aug 2003
    Location
    Bristol, United Kingdom
    Posts
    2,158
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dharma View Post
    The "Connection refused" error suggests that there is something going wrong on the other end - you said you can connect manually so that's obviously not it. Are you perhaps connecting on the wrong port?
    I'm not sure. The server is internal so I cannot provide a link as reference. I'm just viewing it as a normal URL in both and receiving an error on the work server.

  4. #4
    SitePoint Enthusiast Dharma's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm wondering if a proxy or firewall in your work setup is blocking attempts - that might cause a connection refused error. Otherwise it's the host you're trying to connect to who, for some reason, chooses to refuse your incoming request for data.

    Best of luck!

  5. #5
    Community Advisor ULTiMATE's Avatar
    Join Date
    Aug 2003
    Location
    Bristol, United Kingdom
    Posts
    2,158
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dharma View Post
    I'm wondering if a proxy or firewall in your work setup is blocking attempts - that might cause a connection refused error. Otherwise it's the host you're trying to connect to who, for some reason, chooses to refuse your incoming request for data.

    Best of luck!
    I really hope not, as I need to connect to this XML file or the website doesn't receive the news updates we need through simplexml_load_file().

    I doubt it's the host I'm connecting to through simplexml_load_file(), mainly because I've tried RSS feeds outside on popular sites like Yahoo and Google. Do you know of any way to get around a blocking like this? I'm trying to connect to an outside RSS feed to output the contents on a HTML page.

  6. #6
    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)
    It wouldn't surprise me that the remote server is blocking access.

    Try using file_get_contents to obtain the data whilst passing a stream context containing a user-agent header...

    PHP Code:
    <?php
    $sData 
    file_get_contents(
        
    'http://www.live.com/news/results.aspx?q=New+York&go=&form=QBNB&format=rss',
        
    false,
        
    stream_context_create(
            array(
                
    'http'=>array(
                    
    'user_agent'=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4'
                
    )
            )
        )
    );
    $oXML simplexml_load_string($sData);
    ?>
    @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.

  7. #7
    Community Advisor ULTiMATE's Avatar
    Join Date
    Aug 2003
    Location
    Bristol, United Kingdom
    Posts
    2,158
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    It wouldn't surprise me that the remote server is blocking access.

    Try using file_get_contents to obtain the data whilst passing a stream context containing a user-agent header...

    PHP Code:
    <?php
    $sData 
    file_get_contents(
        
    'http://www.live.com/news/results.aspx?q=New+York&go=&form=QBNB&format=rss',
        
    false,
        
    stream_context_create(
            array(
                
    'http'=>array(
                    
    'user_agent'=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4'
                
    )
            )
        )
    );
    $oXML simplexml_load_string($sData);
    ?>
    Gave it a try, but still receiving the same problem. As far as I can tell the server admins aren't going to want to provide me access willingly.

  8. #8
    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)
    Okies, what do you getting when try cURL to obtain the data?
    PHP Code:
    <?php
    $rcUrlHandle 
    curl_init('http://www.live.com/news/results.aspx?q=New+York&go=&form=QBNB&format=rss');
    curl_setopt($rcUrlHandle,CURLOPT_RETURNTRANSFER,true);
    $sData curl_exec($rcUrlHandle);
    if(
    curl_errno($rcUrlHandle))
    {
        
    print_r(curl_getinfo($rcUrlHandle));
        
    curl_close($rcUrlHandle);
    }
    else
    {
        
    print_r($sData);
        
    curl_close($rcUrlHandle);
    }
    ?>
    @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.

  9. #9
    Community Advisor ULTiMATE's Avatar
    Join Date
    Aug 2003
    Location
    Bristol, United Kingdom
    Posts
    2,158
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Array ( [url] => http://www.live.com/news/results.aspx?q=New+York&go=&form=QBNB&format=rss [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0 [namelookup_time] => 0.01106 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 0 [upload_content_length] => 0 [starttransfer_time] => 0 [redirect_time] => 0 )
    I'm guessing this means that the contents are being blocked.

  10. #10
    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)
    it looks suspiciously like you are behind a firewall or ISA, could this be true?
    @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.

  11. #11
    Community Advisor ULTiMATE's Avatar
    Join Date
    Aug 2003
    Location
    Bristol, United Kingdom
    Posts
    2,158
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    it looks suspiciously like you are behind a firewall or ISA, could this be true?
    Well, my URL is http://isa.url.com, so I guess the ISA part is true, although I am able to access using www, although in doing this all PHP error messages are removed.

  12. #12
    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)
    ::removed::
    Last edited by AnthonySterling; Dec 2, 2008 at 08:07.
    @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.

  13. #13
    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)
    Actually, scrap that..it doesn't work.

    I've jumped on one of our domain clients who uses a proxy and it returns the same response, so, I would guess it's your local proxy causing you the issues and not the remote site.
    @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.

  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)
    I've tested this and it should work to authorise the request for a local proxy:-
    PHP Code:
    <?php

    /**
     * Obtains remote data via a local proxy
     * requiring authorisation.
     *
     * @param    String    $sURL                The remote target
     * @param    String    $sProxyAddress        The FQDN or IP address of your local proxy
     * @param    Integer    $iProxyPort            The proxy servers port
     * @param    String    $sProxyUsername        Your domain username prefixed by your local domain name eg. DOMAIN\USERNAME
     * @param    String    $sProxyPassword        Your domain password
     * @return    String    
     */
    function file_get_contents_via_proxy$sURL$sProxyAddress$iProxyPort$sProxyUsername$sProxyPassword )
    {
        
    $rcURLhandle curl_init();
        
    curl_setopt($rcURLhandleCURLOPT_URL$sURL);
        
    curl_setopt($rcURLhandleCURLOPT_PROXYAUTHCURLAUTH_NTLM);
        
    curl_setopt($rcURLhandleCURLOPT_PROXYTYPECURLPROXY_HTTP);
        
    curl_setopt($rcURLhandleCURLOPT_PROXYsprintf('%s:%s',$sProxyAddress,$iProxyPort));
        
    curl_setopt($rcURLhandleCURLOPT_PROXYUSERPWDsprintf('%s:%s',$sProxyUsername,$sProxyPassword));
        
    curl_setopt($rcURLhandleCURLOPT_RETURNTRANSFERtrue);
        
    $sData curl_exec($rcURLhandle);
        return ( 
    curl_errno($rcURLhandle) > ) ? false $sData ;
    }

    echo 
    file_get_contents_via_proxy('http://www.google.co.uk/''10.0.0.1'8080'DOMAIN\USERNAME''PASSWORD');

    ?>
    @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
    Community Advisor ULTiMATE's Avatar
    Join Date
    Aug 2003
    Location
    Bristol, United Kingdom
    Posts
    2,158
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    I've tested this and it should work to authorise the request for a local proxy:-
    PHP Code:
    <?php

    /**
     * Obtains remote data via a local proxy
     * requiring authorisation.
     *
     * @param    String    $sURL                The remote target
     * @param    String    $sProxyAddress        The FQDN or IP address of your local proxy
     * @param    Integer    $iProxyPort            The proxy servers port
     * @param    String    $sProxyUsername        Your domain username prefixed by your local domain name eg. DOMAIN\USERNAME
     * @param    String    $sProxyPassword        Your domain password
     * @return    String    
     */
    function file_get_contents_via_proxy$sURL$sProxyAddress$iProxyPort$sProxyUsername$sProxyPassword )
    {
        
    $rcURLhandle curl_init();
        
    curl_setopt($rcURLhandleCURLOPT_URL$sURL);
        
    curl_setopt($rcURLhandleCURLOPT_PROXYAUTHCURLAUTH_NTLM);
        
    curl_setopt($rcURLhandleCURLOPT_PROXYTYPECURLPROXY_HTTP);
        
    curl_setopt($rcURLhandleCURLOPT_PROXYsprintf('%s:%s',$sProxyAddress,$iProxyPort));
        
    curl_setopt($rcURLhandleCURLOPT_PROXYUSERPWDsprintf('%s:%s',$sProxyUsername,$sProxyPassword));
        
    curl_setopt($rcURLhandleCURLOPT_RETURNTRANSFERtrue);
        
    $sData curl_exec($rcURLhandle);
        return ( 
    curl_errno($rcURLhandle) > ) ? false $sData ;
    }

    echo 
    file_get_contents_via_proxy('http://www.google.co.uk/''10.0.0.1'8080'DOMAIN\USERNAME''PASSWORD');

    ?>
    Sorry for the lack of reply. Some idiot set off the fire alarm and by the time we got back in it was time to head home. I did try running the code but for some reason it wouldn't run, although I'm not sure whether that's the code's fault or the server itself...

    I'll give it a fresh run tomorrow. Thanks for all the help so far!

  16. #16
    Community Advisor ULTiMATE's Avatar
    Join Date
    Aug 2003
    Location
    Bristol, United Kingdom
    Posts
    2,158
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the late reply. It's been a hectic couple of days.

    The code works! I spoke to one of the server admins and he told me to connect through their proxy, so after adding that in and connecting I am now able to run my code.

    Thanks for all the help!

  17. #17
    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)
    That's great news, happy, er, XML'ing?
    @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.


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
  •