1

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");
1
  • What error(s) do you get? Commented Oct 21, 2014 at 17:23

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

While you did address why it doesn't work, he actually wants the XML response. Could you show how to use a stream context with header variants before calling the file_get_contents method
@Ohgodwhy Am checking the API of autoit to see what it wants in the request.
He said in the question, you just need to set xml as an accept type in the header. See my answer...
2

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! :)

1 Comment

dude, you are such a lifesaver! after hours of searching, your snippet solves my problem! THANKS A GAZILLION!!!
1

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

2 Comments

You're using fopen for no reason. This doesn't address the returned data type, either. It's simply just copy+pasta.
But your header is still wrong, you see, the data is returned in JSON format, if you want to read the data as xml you need to change your CURLOPT_HEADER to use an array that declares the content-type as application/xml, then this will work and I'll remove my -1 =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.