【发布时间】:2016-05-23 16:15:28
【问题描述】:
我正在尝试从 URL 获取 XML 文件。 但我只从文件中获取文本,没有语法。
我的代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://services.mobile.de/search-api/search?country=DE&sort.field=makeModel&sort.order=ASCENDING');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '. base64_encode("username:password"), 'Accept:
application/xml'));
$xml = new SimpleXMLElement(curl_exec($ch));//line 14
curl_close($ch);
print_r($xml);
我的输出:
....sensorsBlackEuro5PetrolManual gearboxAutomatic air conditioningUsed vehicle150.90525114.812090CarSmall CarVolkswagenPoloCentral lockingElectric windowsImmobilizerPower Assisted SteeringABSESPFull Service HistoryElectric side mirrorOn-board computerCD playerTuner/radioIsofix (child seat anchor points)Parking sensor
Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found in /Applications/XAMPP/xamppfiles/htdocs/test.php on line 14
Warning: SimpleXMLElement::__construct(): 1 in /Applications/XAMPP/xamppfiles/htdocs/test.php on line 14
Warning: SimpleXMLElement::__construct(): ^ in /Applications/XAMPP/xamppfiles/htdocs/test.php on line 14
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /Applications/XAMPP/xamppfiles/htdocs/test.php:14 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/test.php(14): SimpleXMLElement->__construct('1') #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/test.php on line 14
更新:
我现在在浏览器中得到了正确的字符串:
echo '<pre>';
echo htmlspecialchars(print_r(curl_exec($ch), true));
echo '</pre>';
但是当我尝试使用时:
$sxe = simplexml_load_string($re);
print_r($sxe);
我只知道这个:
SimpleXMLElement Object ( )
【问题讨论】:
-
当你在浏览器上打开xml url时,你能看到有效的xml吗?
-
您知道 API 返回 XML 的事实吗?如果使用 curl 从命令行发送请求会发生什么?
-
您确定要从您的请求中获取 XML 文件吗?
$response = curl_exec($ch); echo htmlspecialchars($response);向您展示了什么? -
@hakkikonu 我通过浏览器获得了正确的 XML。
-
@JimGarrison 我想是的。 services.mobile.de/manual/search-api.html