根据标题,我使用cURL连接到我的webservice,然后以XML格式输出响应。根据我的检查,服务器上启用了cURL,但是在$xml
的响应中没有显示任何内容。
define('SERVICE_URL', 'http://www.myurl.com/webservices/users');
define('USERNAME', 'user');
define('PASSWORD', 'pass');
//define('KEY', 'YOUR_KEY/SID_HERE');
$post = array(
'UserID' => '5',
'Name' => 'John',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, SERVICE_URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($result);
echo $xml;
首先,检查$result是否确实给了您一个结果。如果不是,那么问题不在于SimpleXMLElement,而在于curl调用。
第二,创建一个HTML表单,将数据发送到URL,并检查它是否实际将数据发送回来。如果你没有得到数据,那么问题出在服务上,而不是你的curl调用。
最后,如果您在$result中获得数据,那么检查SimpleXMLElement调用中的异常:
try {
$xml = new SimpleXMLElement($result);
}
catch( Exception $e ) {
echo "found an exception: " . $e;
}