简体   繁体   English

PHP-cURL不输出XML数据

[英]PHP - cURL not outputting XML data

As per the title, I'm using cURL to connect up to my webservice which should then output the response in XML format. 按照标题,我正在使用cURL连接到我的Web服务,然后应该以XML格式输出响应。 cURL is enabled on the server as per my check, however it isn't displaying anything in the response of $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;

First, check that $result is actually giving you a result. 首先,检查$ result实际上是否给您结果。 If it isn't, then your problem isn't with SimpleXMLElement, it's with your curl call. 如果不是,那么您的问题就出在SimpleXMLElement上,而不是您的curl调用。

Second, create an HTML form that sends your data to your URL and check to see that it's actually sending data back. 其次,创建一个HTML表单,将您的数据发送到您的URL,并检查它实际上是在向后发送数据。 If you aren't getting data, then the problem is with the service, not your curl calls. 如果没有获取数据,则问题出在服务上,而不是curl调用上。

Finally, if you're getting data in $result, then check for exceptions in your SimpleXMLElement call: 最后,如果要获取$ result中的数据,请在SimpleXMLElement调用中检查异常:

try {
  $xml = new SimpleXMLElement($result);
}
catch( Exception $e ) {
  echo "found an exception: " . $e;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM