How can I upload using PHP an XML file redirected in some strange way?

The file that I am trying to download from my PHP script is as follows:

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

But I cannot do this using file_get_contents()and cURL. I get an errorObject reference not set to an instance of an object.

Any idea how to do this?

Thanks, Pablo.

Updated to add code:

$url = "http://www.navarra.es/appsext/DescargarFichero/default.aspx?codigoAcceso=OpenData&fichero=Farmacias/Farmacias.xml";
$simple = simplexml_load_file(file_get_contents($url));
foreach ($simple->farmacia as $farmacia)
{
    var_dump($farmacia);
}

And the solution thanks to @Gordon:

$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);
+3
source share
2 answers

You do not need to cURL, but not file_get_contentsload XML into any of the PHP DOMs based on XML parsers .

, HTTP-. php.ini, libxml :

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

XML , file_get_contents. :

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

Live Demo

+5

, @Gordon, 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);

... , . URL- , file_get_contents() , :

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;
}

, ?

, .

0

All Articles