使用PHP从URL读取XML

时间:2014-10-21 17:13:24

标签: php xml content-type

我想从xml提要中读取一些数据并使用php在网页上显示。这是供稿网址: http://api.autoit.dk/car/GetCarsExtended/391B093F-BB4A-45AA-BEFF-7B33842401EA

我被告知该服务能够返回JSON和XML,具体取决于客户端在标头内容类型中接受的内容。我一直试图使用XML,但是我遇到了一些错误:

  

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

应该很简单,但我没有运气。有人会帮助我直截了当吗? 我的test.php文件看起来很简单:

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

3 个答案:

答案 0 :(得分:2)

如果您运行以下代码,您将看到您实际上正在获取JSON作为响应:

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

输出:

  

[{"ComId":4712,"Husnr":10,"Navn":"Flexto A/S","By":"Støvring","Postnr":9530,"Adresse":"Juelstrupparken","Email":"info@flexto.dk","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,

(等)

SimpleXML无法将JSON转换为对象,因此显然会抛出错误。

如果要使用JSON数据而不是XML,可以简单地解码JSON:

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

$data的一部分:

(
    [0] => Array
        (
            [ComId] => 4712
            [Husnr] => 10
            [Navn] => Flexto A/S
            [By] => Støvring
            [Postnr] => 9530
            [Adresse] => Juelstrupparken
            [Email] => info@flexto.dk

如果您需要XML数据,则需要在调用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 );

立即输出:

<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" />

(等)

有关如何为stream_context_create设置标头的详细信息,请参阅file_get_contents

答案 1 :(得分:1)

可能重复:Loading a remote xml page with file_get_contents()

我会使用cURL来做这样的事情。

以下是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 :(得分:0)

Curl可能是更好的选择,但您也可以在file_get_contents中设置相应的标头:

$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");

当然,你可以改用JSON! :)