我真的希望有人可以帮助我解决这个问题......
我需要使用 php 对 RESTful API 进行 xml POST,但我完全不知道从哪里开始。
我可以构建 xml,但是如何发布呢?我不能使用 cURL 库。
您可以使用 file_get_contents()。allow_url_fopen
必须在 php.ini 上设置。
$context = stream_context_create(array('http'=>array(
'method' => 'POST'
'content' => $myXMLBody
)));
$returnData = file_get_contents('http://example.com/restfulapi', false, $context);
这是可能的,因为 PHP 使用自己的包装器抽象了流操作。为流操作函数(如 file_get_contents())设置上下文允许您配置 PHP 如何处理它。
method
除了和之外,还有更多参数content
。您可以设置请求标头、代理、处理超时和一切。请参阅手册。
几天前我遇到了同样的问题,最后有人想出了这个解决方案,但不要使用 $this->_request 来获取发布请求,因为它不适用于 xml,至少在我的情况下.
$service_url1 = 'http://localhost/restTest/respond/';
$curl1 = curl_init($service_url1);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
$arr=array("key"=>$xml);
curl_setopt($curl1, CURLOPT_POST, 1);
curl_setopt($curl1, CURLOPT_POSTFIELDS,$arr);
echo $curl1_response = curl_exec($curl1);
curl_close($curl1);