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

前端 未结 2 861
醉酒成梦
醉酒成梦 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:53

    I have been using the solution given for @Gordon and it was working perfectly in localhost:

    $url = "http://www.navarra.es/appsext/DescargarFichero/default.aspx?codigoAcceso=OpenData&fichero=Farmacias/Farmacias.xml";
    $file = file_get_contents($url, FALSE, stream_context_create(array('http' =>array('user_agent' => 'php' ))));
    $simple = simplexml_load_string($file);
    

    But when I have uploaded all the files to the server... surprise, as always. I started to get the error URL file-access is disabled in the server configuration in so I have changed all the file_get_contents() for this code which I have found here:

    function get_content($url)
    {
    $ch = curl_init();
    
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_USERAGENT, "Googlebot/2.1...");
    
    ob_start();
    
    curl_exec ($ch);
    curl_close ($ch);
    $string = ob_get_contents();
    
    ob_end_clean();
    
    return $string;
    }
    

    Would you think is it a good approach?

    Thanks, Pablo.

提交回复
热议问题