使用curl函数返回的xml

时间:2014-07-14 23:31:30

标签: php xml curl ioerror

我有一个curl函数,它连接到API并返回xml。

我从另一个脚本调用此函数,并希望通过xml并选择一些URL,但是我得到一个I / O错误,所以我认为我没有正确处理xml。

这是卷曲功能

function &connect($url) {

    //If token is not set skip to else condition to request a new token 
    if(!empty($_SESSION['token'])) {


        //Initiate a new curl session
        $ch = curl_init($url);
        //Don't require header this time as curl_getinfo will tell us if we get HTTP 200 or 401
        curl_setopt($ch, CURLOPT_HEADER, 0); 
        //Provide Token in header
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$_SESSION['token']));
        //Execute the curl session
        $data = curl_exec($ch);

        //Close the curl session
        curl_close($ch);

        return $data;

    } else {

        //Run the getToken function above if we are not authenticated
        getToken($url);

        return 'error';

    }

}

这就是所谓的

//build url creates the api url required based on parameters passed into GET
$link = build_url;
//call the connect function and pass it the built link
$xml = connect($link);
//load the returned xml
$oxml = simplexml_load_file($xml);

connect函数肯定是获取xml,因为我在函数中回显了它,当我在浏览器中运行脚本时,它将xml输出到屏幕以及“Warning:simplexml_load_file():I / O警告:未能在“

中加载外部实体”1“

我不确定我缺少什么: - (

1 个答案:

答案 0 :(得分:0)

simplexml_load_file();通过传递文件名从文件中加载XML。

由于您已经拥有XML字符串,因此您需要simplexml_load_string();

修改

此外,您需要使用curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);告诉curl返回响应,而不是仅返回状态代码。您看到的1是状态代码。