Ovesen
Ovesen

Reputation: 23

Reading XML from URL with PHP

I want to read some data from an xml feed and show it on a webpage using php. This is the feed URL: http://api.autoit.dk/car/GetCarsExtended/391B093F-BB4A-45AA-BEFF-7B33842401EA

I have been told that the service is able to return both JSON and XML, depending on what the client accepts in the header content type. I have been trying to go with XML, but I get some errors:

Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in /home/www/flexto.dk/test.php on line 3

Warning: simplexml_load_string(): [{"ComId":4712,"Husnr":10,"Navn":"Flexto A/S","By":"Støvring","Postnr":9530,"Ad in /home/www/flexto.dk/test.php on line 3

Warning: simplexml_load_string(): ^ in /home/www/flexto.dk/test.php on line 3 Error: Cannot create object

See my online test script.

It should be pretty simple, but I haven't had luck with it. Would anyone help me get it straight? My test.php document looks as simple as this:

$xml = simplexml_load_string( file_get_contents( 'http://api.autoit.dk/car/GetCarsExtended/391B093F-BB4A-45AA-BEFF-7B33842401EA' ) )
       or die("Error: Cannot create object");

Upvotes: 1

Views: 5387

Answers (3)

cpk
cpk

Reputation: 809

Potential duplicate: Loading a remote xml page with file_get_contents()

I would use cURL for something like this.

Here is a slightly modified example from http://php.net/manual/en/curl.examples-basic.php :

<?php

$ch = curl_init();

$options = array(CURLOPT_URL => 'http://www.example.com/',
                 CURLOPT_HEADER => true,
                 CURLOPT_HTTPHEADER => 'Content-type: application/xml'
                );

curl_setopt_array($ch, $options);

$data = curl_exec($ch);
curl_close($ch);

 if($data) {
    $xml = new SimpleXMLElement($data);

    echo $xml;
 }
?>

Upvotes: 1

i alarmed alien
i alarmed alien

Reputation: 9520

If you run the following code, you'll see that you're actually getting JSON as response:

$resp = file_get_contents( 'http://api.autoit.dk/car/GetCarsExtended/391B093F-BB4A-45AA-BEFF-7B33842401EA' );
echo $resp;

Output:

[{"ComId":4712,"Husnr":10,"Navn":"Flexto A/S","By":"Støvring","Postnr":9530,"Adresse":"Juelstrupparken","Email":"[email protected]","Telefon":"96365225","KoeretoejId":641321,"Abs":true,"Accelerationsevne0Til100Kmt":null,"AirbagsAntal":6,"Beskrivelse":"- og meget mere.\nDette er et flexleasingtilbud, udbetaling 45.000,- depot 16.000,- leasingydelse 5.700,- alt excl. moms. Restværdi efter 3 år 121.000 excl. afgift.","BreddeMm":1841,

(etc.)

SimpleXML is not able to convert JSON into an object, so it's obviously throwing an error.

If you want to use the JSON data instead of XML, you can simply decode the JSON:

$data = json_decode($resp, true); // or json_decode($resp) for objects rather than arrays

Part of $data:

(
    [0] => Array
        (
            [ComId] => 4712
            [Husnr] => 10
            [Navn] => Flexto A/S
            [By] => Støvring
            [Postnr] => 9530
            [Adresse] => Juelstrupparken
            [Email] => [email protected]

If you want the XML data, you'll need to set the appropriate headers when calling file_get_contents:

$opts = array(
    'http' => array(
        'header' => "Accept: application/xml"
    )
);

$context = stream_context_create($opts);

$response = file_get_contents( 'http://api.autoit.dk/car/GetCarsExtended/391B093F-BB4A-45AA-BEFF-7B33842401EA', NULL, $context );

Output now:

<ArrayOfDealerCarExtended xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/AutoITApis.Controllers"><DealerCarExtended><Abs>true</Abs><Accelerationsevne0Til100Kmt i:nil="true" />

(etc.)

See stream_context_create for more information on how to set up headers for file_get_contents.

Upvotes: 2

rjdown
rjdown

Reputation: 9227

Curl might be a better option, but you can set the appropriate headers in file_get_contents too:

$options = array(
    'http' => array(
        'method' => "GET",
        'header' => "Accept: application/xml\r\n"
    )
);
$context = stream_context_create($options);
$url = 'http://api.autoit.dk/car/GetCarsExtended/391B093F-BB4A-45AA-BEFF-7B33842401EA';
$xml = simplexml_load_string(file_get_contents($url, false, $context)) or die("Error: Cannot create object");

Of course, you could just use JSON instead! :)

Upvotes: 2

Related Questions