我使用RESTful webservice,它显示了一些xml。
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <users>
- <user>
<id>1</id>
<name>Mahesh</name>
<profession>Teacher</profession>
</user>
</users>
我想添加新的xml,发送到网络服务。
$xml =
'<users>
<user>
<id>3</id>
<name>hangga</name>
<profession>IT</profession>
</user>
</users>';
这是我的php代码:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://localhost:18080/UserManagement/rest/UserService/users");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); //setting content type header
curl_setopt($curl, CURLOPT_POST, $xml);//Setting raw post data as xml
$result = curl_exec($curl);
curl_close($curl);
print($result);
但没有任何反应,Web服务没有显示新的XML。我的问题如何添加新的XML并使用PHP将其发送到Web服务?我应该使用cUrl吗?有另一种方式吗?我真的需要帮助,答案非常感谢。谢谢大家
答案 0 :(得分:0)
$url = 'http://webservice/example/user';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/xml',
'Content-Length: ' . strlen($data))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$responses = curl_exec($ch);
curl_close($ch);