[英]PHP: send POST request then read XML response?
我正在嘗試編寫一個PHP腳本,將POST請求發送到遠程服務器,然后解析XML響應。
我可以做POST請求,但是很難(從其他SO問題)解決如何解析XML響應。
我當前的代碼告訴我: Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "1" in /Users/simon/usercreate.php on line 46
- simplexml_load_file($response)
一行。
我正在使用本地服務器,不確定這是否有所作為。 碼:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
$response = curl_exec ($curl);
curl_close ($curl);
echo $response;
$rxml = simplexml_load_file($response);
echo $rxml->title;
我究竟做錯了什么?
使用simplexml_load_string而不是simplexml_load_file
您必須設置cURL選項才能返回傳輸
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
您想要加載字符串,而不是加載文件。
// Instead of
$rxml = simplexml_load_file($response);
// You want
$rxml = simplexml_load_string($response);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.