How can I download using PHP a XML file redirected in some weird way?

前端 未结 2 863
醉酒成梦
醉酒成梦 2020-12-06 21:07

The file which I\'m trying to download from my PHP script is this one:

http://www.navarra.es/appsext/DescargarFichero/default.aspx?codigoAcceso=OpenData&         


        
2条回答
  •  旧巷少年郎
    2020-12-06 21:46

    You dont need cURL, nor file_get_contents to load XML into any of PHP's DOM Based XML parsers.

    However, in your particular case, the issue seems to be that the server expects a user agent in the http request. If the user agent is not set in your php.ini, you can use the libxml functions and provide it as a stream context:

    libxml_set_streams_context(
        stream_context_create(
            array(
                'http' => array(
                    'user_agent' => 'php'            
                )
            )
        )
    );
    
    $dom = new DOMDocument;
    $dom->load('http://www.navarra.es/app…/Farmacias.xml');
    echo $dom->saveXml();
    

    Live Demo

    If you dont want to parse the XML file afterwards, you can use file_get_contents as well. You can pass the stream context as the third argument:

    echo file_get_contents(
        'http://www.navarra.es/apps…/Farmacias.xml',
        FALSE,
        stream_context_create(
            array(
                'http' => array(
                    'user_agent' => 'php'            
                )
            )
        )
    );
    

    Live Demo

提交回复
热议问题