PHP向REST风格Web服务发送Post请求

REST风格的Web服务以资源为中心,十分适合操作数据,利用了Http是一种程序协议,而不需要Soap。

所以我们可以使用Curl库来向一个Web服务发送一个Post请求,数据是XML格式。

$xml_data = ‘<?xml version=”1.0″ encoding=”utf-8″?>’
                   .’<TestInfo xmlns=”http://demo.dfe.com”>’
                   .’<test>’.$info.’</test>’
                   .’</TestInfo>’;

注意加入命名空间xmlns。

$ch = curl_init();

        curl_setopt($ch,CURLOPT_URL,$url);

        curl_setopt($ch,CURLOPT_POST,1);

        curl_setopt($ch,CURLOPT_HTTPHEADER,array(‘Content-Type: text/xml’));


        curl_setopt($ch,CURLOPT_POSTFIELDS,$xml_data);

        $result = curl_exec($ch);

        curl_close($ch);


消息头中要加入内容类型为text/xml。
VN:F [1.9.7_1111]
留下你的评价吧
Rating: 8.4/10 (8 votes cast)
PHP向REST风格Web服务发送Post请求, 8.4 out of 10 based on 8 ratings

相关文章

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*