这几天做webservice 接口,废话不多说了,直接上代码:
1、提交页面
<?php
$xml_data ='
<wms_interface>
<com_no>siyue</com_no>
<interface_type>push_order</interface_type>
<order>
<order_no>H201305300001</order_no>
<name>张三</name>
<number>13770629733,025-57023099</number>
<province>江苏省</province>
<city>南京市</city>
<county>玄武区</county>
<addr>估衣廊38号2楼3308室</addr>
<order_amount>689.50</order_amount>
<cod_payment>500</cod_payment>
<order_memo>送前打电话</order_memo>
<items>
<item>
<barcode>686656272367623</barcode>
<item_name>p57</item_name>
<item_count>2</item_count>
<item_price>99</item_price>
<item_memo></item_memo>
</item>
<item>
<barcode>676656222367623</barcode>
<item_name>突击12</item_name>
<item_count>1</item_count>
<item_price>239</item_price>
<item_memo></item_memo>
</item>
</items>
</order>
</wms_interface>
';
function request_by_curl($remote_server, $post_string)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Jimmy's CURL Example beta");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$a=request_by_curl("http://localhost:88/daifahuo/order_request_add.php",$xml_data); /post发送请求
print_r($a);/接收返回
?>
2、接收页面
<?php
$i=$_POST;//post提交获取
//$b=$_GET;//get 提交获取
//$c=$HTTP_RAW_POST_DATA;//http协议
if($i){
$i=implode('',$i);
$xml=simplexml_load_string($i);
$arr=objectsIntoArray($xml);
输出接收数据
print_r($arr);
exit();
}
//接收失败
echo "fail";
//解析为数据方式输出方法
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
;
?>
访问返回的结果(乱码调编码):