CURL 返回完整的字符串响应而不是 xml

sim*_*guy 5 php xml curl

我正在尝试像这样在 PHP 中使用 CURL 提取数据

$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
Run Code Online (Sandbox Code Playgroud)

但是当响应应该是 xml 格式时,我得到了完整字符串的响应,当我打印响应变量的类型时,它会打印字符串。我尝试使用 Postman 检查响应,它在 XML 模式下返回正确的格式:

xml格式

当我在 Postman 中切换到预览模式时,它显示的结果与我在 php 中的 CURL 响应显示的结果相同,它是完整的字符串:

字符串格式

han*_*rik 5

最有可能的是,您得到了正确的响应,只是您的浏览器没有按照您预期的方式呈现它。如果在代码的顶部添加,会发生什么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
Run Code Online (Sandbox Code Playgroud)