curl php中的xml输出

时间:2014-09-10 06:22:59

标签: php xml curl

我正在使用php中的curl库从api读取数据。当我在POSTMAN中调用api时,work.output出现在我需要的xml中。但是当我用带有curl库的标题调用那个url时,我得到的浏览器输出不是xml格式。但是我想要它是xml格式。

请检查下面的代码并帮助纠正。

    <?php
    header('Content-type: application/xml');
    function httpGet($url)
    {
        $ch = curl_init();  

        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    //  curl_setopt($ch,CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'header1: value1',
        'header2:value2'
        ));

        $output=curl_exec($ch);

        curl_close($ch);
        return $output;
    }

    $data= httpGet("https://somehost/api/id.xml");
    $xml = new SimpleXMLElement($data);
    echo $xml->asXML();
    ?>

0 个答案:

没有答案