我真的希望有人可以帮我这个......
我需要使用php对RESTful API进行xml POST,但我完全不知道从哪里开始。
我可以构建xml,但是如何发布呢?我不能使用cURL库。
答案 0 :(得分:1)
您可以使用file_get_contents()。必须在php.ini上设置allow_url_fopen
。
$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
。您可以设置请求标头,代理,处理超时和一切。请参阅the manual。
答案 1 :(得分:1)
几天前我遇到了同样的问题,最后有人想出了这个解决方案,但是不要使用$ 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);