卷曲返回满弦的响应不是XML

问题描述:

我想拉使用curl数据在PHP这样卷曲返回满弦的响应不是XML

$ch = curl_init(); 
$headers = array('Content-type: text/xml',"openapikey:da21d9s56ekr33"); 
curl_setopt($ch, CURLOPT_URL, "http://api.test.co.un/rest/cateservice/category"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_FAILONERROR,1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 15); 
$return = curl_exec($ch); 
print_r($return); 
print_r(gettype($return)); //prints string 

,但我得到了满弦的响应时的响应应该是XML格式的,并当我打印响应变量的类型时,它会打印字符串。我试着用邮差检查响应并返回XML模式的正确格式:

xml format

,当我更改为邮差预览模式,它显示了同样的结果在PHP显示了我的卷曲响应,这是满弦:

string format

+0

我没有发现它有必要指定标题时检索卷曲的XML,尝试没有头。我简单地使用:$ curl = curl_init(“URL”); curl_setopt($ curl,CURLOPT_RETURNTRANSFER,1);; $结果= curl_exec($ curl) – mayersdesign

+0

@mayersdesign我需要把标题使用API​​令牌 –

+0

没有直接关系到你的问题,但使用https://github.com/guzzle/guzzle它更容易更快,最有可能会解决你所有的问题。 –

最有可能的,你得到正确的响应,它只是你的浏览器不渲染它,你预料到的方式。如果在代码顶部添加header("content-type: text/plain;charset=utf8");,会发生什么情况?我的猜测是,它会按照您的预期呈现代码。或者,html编码它,就像

function hhb_tohtml(string $str):string 
{ 
    return htmlentities($str, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE | ENT_DISALLOWED, 'UTF-8', true); 
} 
print_r(hhb_tohtml($return)); 
print_r(gettype($return)); //prints string