curl_xml.html:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>cURL提交XML数据</title> 6 <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> 7 <script> 8 $(function(){ 9 $(‘input[type="button"]‘).click(function(){ 10 //alert(decodeURIComponent($(‘form‘).serialize())); //decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码 11 $.ajax({ 12 url : ‘curl_xml.php‘, 13 type : ‘post‘, 14 data : $(‘form‘).serialize(), 15 success : function(data, status, xhr){ 16 $(‘#box‘).html(data); 17 } 18 }); 19 }); 20 }) 21 </script> 22 </head> 23 <body> 24 <form> 25 商品名称:<input type="text" name="goods_name"><br/> 26 商品价格:<input type="text" name="goods_price"><br/> 27 商品分类:<select name="goods_brand"> 28 <option value="电脑">电脑</option> 29 <option value="平板">平板</option> 30 <option value="手机">手机</option> 31 <option value="大哥大">大哥大</option> 32 </select> 33 <br/> 34 是否发货:<input type="radio" name="issend" value="是"> 是 <input type="radio" name="issend" value="否"> 否<br/> 35 <input type="button" value="提交"> 36 </form> 37 <div id="box"></div> 38 </body> 39 </html>
界面:
curl_xml.php:
1 <?php 2 $goods_name = $_POST[‘goods_name‘]; 3 $goods_price = $_POST[‘goods_price‘]; 4 $goods_brand = $_POST[‘goods_brand‘]; 5 $issend = $_POST[‘issend‘]; 6 7 $xml = <<<xml 8 <?xml version="1.0" encoding="utf-8"?> 9 <goods> 10 <goodsname>$goods_name</goodsname> 11 <goodsprice>$goods_price</goodsprice> 12 <goodsbrand>$goods_brand</goodsbrand> 13 <issend>$issend</issend> 14 </goods> 15 xml; 16 17 $url = "http://localhost/test/curl_xml_deal.php"; 18 19 $ch = curl_init(); 20 21 curl_setopt($ch, CURLOPT_URL, $url); 22 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 23 curl_setopt($ch, CURLOPT_POST, true); 24 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 25 26 $res = curl_exec($ch); 27 28 curl_close($ch); 29 30 echo $res; 31 ?>
curl_xml_deal.php:
1 <?php 2 $xml = file_get_contents("php://input"); 3 $sxe = simplexml_load_string($xml); 4 var_dump($sxe); 5 //echo $sxe[0]->goodsname; //商品名称 6 ?>
结果:
时间: 2024-11-05 23:40:39