AJAX+cURL+SimpleXMLElement处理数据

时间:2022-04-12 11:30:55

curl_xml.html:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>cURL提交XML数据</title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(function(){
$('input[type="button"]').click(function(){
//alert(decodeURIComponent($('form').serialize())); //decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码
$.ajax({
url : 'curl_xml.php',
type : 'post',
data : $('form').serialize(),
success : function(data, status, xhr){
$('#box').html(data);
}
});
});
})
</script>
</head>
<body>
<form>
商品名称:<input type="text" name="goods_name"><br/>
商品价格:<input type="text" name="goods_price"><br/>
商品分类:<select name="goods_brand">
<option value="电脑">电脑</option>
<option value="平板">平板</option>
<option value="手机">手机</option>
<option value="大哥大">大哥大</option>
</select>
<br/>
是否发货:<input type="radio" name="issend" value="是"> 是 <input type="radio" name="issend" value="否"> 否<br/>
<input type="button" value="提交">
</form>
<div id="box"></div>
</body>
</html>

界面:AJAX+cURL+SimpleXMLElement处理数据

curl_xml.php:

 <?php
$goods_name = $_POST['goods_name'];
$goods_price = $_POST['goods_price'];
$goods_brand = $_POST['goods_brand'];
$issend = $_POST['issend']; $xml = <<<xml
<?xml version="1.0" encoding="utf-8"?>
<goods>
<goodsname>$goods_name</goodsname>
<goodsprice>$goods_price</goodsprice>
<goodsbrand>$goods_brand</goodsbrand>
<issend>$issend</issend>
</goods>
xml; $url = "http://localhost/test/curl_xml_deal.php"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); $res = curl_exec($ch); curl_close($ch); echo $res;
?>

curl_xml_deal.php:

 <?php
$xml = file_get_contents("php://input");
$sxe = simplexml_load_string($xml);
var_dump($sxe);
//echo $sxe[0]->goodsname; //商品名称
?>

结果:

AJAX+cURL+SimpleXMLElement处理数据