我正在试图解析一个xml文件但是没有工作。我试过这个:
$url = /* Some like this */'https://test.httpapi.com/api/customers/signup.xml?auth-userid=0&api-key=key&username=email@email.com&passwd=password9&name=name &company=company&address-line-1=address-line-1&city=city&state=state&country=US&zipcode=0000&phone-cc=0&phone=000000&lang-pref=en';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-type: text/xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close ($ch);
$status = $info['http_code'];
echo curl_error($ch);
echo '<pre>';
$xml = simplexml_load_string($response);
print_r($xml);
我明白了:
SimpleXMLElement Object ( [body] => SimpleXMLElement Object ( [h1] => 400 Bad request ) )
网址是正确的,我可以看到:
<response>
<status>ERROR</status>
<message>Required parameter missing: name</message>
</response>
我也试过这个并收到“文件错误”:
$xml = file_get_contents($url) or die ('file error');
或
$xml = simplexml_load_string($url) or die ('file error');
或
$xml = simplexml_load_file($url) or die ('file error');
我如何解析它?
答案 0 :(得分:0)
错误讯息:
首先:
$xml = file_get_contents($url) or die ('1file error');
Warning: file_get_contents(myurl) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad request in /myserver on line 354
第二
$xml = simplexml_load_string($url) or die ('2file error');
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found in /myserver on line 356
Warning: simplexml_load_string() [function.simplexml-load-string]: myurl in /myserver on line 356
Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /myserver on line 356
2file error
第三名:
$xml = simplexml_load_file($url) or die ('3file error');
Warning: simplexml_load_file(myurl) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in /myserver on line 358
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "myurl" in /myserver on line 358
3file error
答案 1 :(得分:0)
对test.httpapi的所有请求都需要GET,即使它们传统上是POST。