在我的网站上,我正在向一个支付网站(websitechecks.com)提交一个html表单,该网站通过XML响应对我进行响应。XML响应显示在外部链接上(https://api.webmasterchecks.com/payments/add)正常表单提交后(通过单击提交按钮)。我想读取XML响应并更新我的数据库。我的编码语言是PHP。
HTML表单:
<form name="payment_form" action="https://api.webmasterchecks.com/payments/add" method="post">
<input type="text" name="client_id" value="****"></input>
<input type="text" name="akey" value="**********"></input>
<input type="text" name="method_id" value="2"></input>
<input type="text" name="payee" value="ABCD EFGH"></input>
<input type="text" name="amount" value="1.00"></input>
<input type="text" name="postage_id" value="6"></input>
<input type="text" name="reference" value="Payment"></input>
<input type="text" name="street_addr" value="51 ABCD"></input>
<input type="text" name="city" value="XYZ"></input>
<input type="text" name="state" value="NJ"></input>
<input type="text" name="country" value="United States"></input>
<input type="text" name="zip" value="01544"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
我尝试使用jquery-post提交表单并获取json数据。但是表单没有提交(控制台中也没有出现任何错误)。代码如下。
$(function(){
$("form[name=payment_form]").submit(function(){
$.post($(this).attr("action"), $(this).serialize(), function(jsonData){
alert(jsonData);
}, "json");
return false;
});
});
我也尝试过使用旋度,但没有成功。它显示API键是无效的,因为使用上面的html表单,我得到了响应。CURL代码如下所示。
$client_id = "****";
$api_key = "*****************************";
$output_url = "https://api.webmasterchecks.com/payments/add";
$output_transaction = "client_id=$client_id&";
$output_transaction .= "akey=$api_key&";
$output_transaction .= "method_id=2&";
$output_transaction .= "payee=ABCD EFGH&";
$output_transaction .= "amount=1.00&";
$output_transaction .= "postage_id=6&";
$output_transaction .= "reference=Payment&";
$output_transaction .= "street_addr=FGHFHGFG&";
$output_transaction .= "city=JHGJHG&";
$output_transaction .= "state=NJ&";
$output_transaction .= "country=United States&";
$output_transaction .= "zip=54545454";
ob_start();
$ch = curl_init ($output_url);
curl_setopt ($ch, CURLOPT_VERBOSE, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $output_transaction);
curl_exec ($ch);
curl_close ($ch);
$process_result = ob_get_contents();
ob_end_clean();
如何使用XML响应更新数据库?
提前谢谢。
编辑
我的api密钥上几乎没有加"+"号。下面给出了api密钥的示例格式。
q5Un5ObLZs2ovY2COW+LvHzEVdUy0kR3u6dPwh/p+wzlbh80vONBbo+otLpBwPqnvP/VjhglfFos51sLFDpUHi+6GnVbLtR3ATjSz9trGoKLFgrK/ostPUG4t9XV1EdS10JzVZFscIxUu2LVmJN9NVpCgaD9NaA
这里需要做的是将数据发送到本地脚本,然后将其打包为curl请求,以便通过其API与远程服务器交互。
<form name="payment_form" action="local_script.php" method="post">
<input type="text" name="method_id" value="2"></input>
<input type="text" name="payee" value="ABCD EFGH"></input>
<input type="text" name="amount" value="1.00"></input>
<input type="text" name="postage_id" value="6"></input>
<input type="text" name="reference" value="Payment"></input>
<input type="text" name="street_addr" value="51 ABCD"></input>
<input type="text" name="city" value="XYZ"></input>
<input type="text" name="state" value="NJ"></input>
<input type="text" name="country" value="United States"></input>
<input type="text" name="zip" value="01544"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
注意,我已经从表单中删除了client_id和api_key,因为它们永远不应该像这样向公众显示
local_script.php
$api_url = "https://api.webmasterchecks.com/payments/add";
$client_id = "****";
$api_key = "*****************************";
$_POST['client_id'] = $client_id;
$_POST['akey'] = $api_key;
$post_data = http_build_query($_POST);
$xml = new DOMDocument();
$ch = curl_init ($api_url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//return xml
curl_setopt($ch, CURLOPT_HEADER, FALSE);//we only need the body
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$xml->loadXML(curl_exec($ch));
curl_close ($ch);
//for testing echo xml
echo $xml->saveXML();
//you need now to parse the xml before adding info to db
$name = $xml->getElementsByTagName('name')->item(1)->nodeValue;
$email = $xml->getElementsByTagName('email')->item(1)->nodeValue;
这就是我要做的,因为我精通用于解析和编写xml的DOMDocument,所以您可以使用DOMDocument手册进行解析http://www.php.net/manual/en/class.domdocument.php或使用SIMPLEXMLhttp://php.net/manual/en/book.simplexml.php无论哪种方式,都需要解析xml以将数据添加到数据库中。
也许您还可以使用ajax向该URL发送数据和从该URL获取数据。我创建了一个显示地图和标记数据的简单页面。我已取回数据使用ajax。对我的ajax调用的响应是xml。所以之后得到响应后,我简单地解析了xml。
例如
var url="mapData.php";var request=getRequestObj();request.open("GET",url,true);request.onreadystatechange=函数(){if(request.readyState==4){var xmlDoc=request.responseXML;//获得标记数组并循环通过var marks=xmlDoc.docentElement.getElementsByTagName("marker");for(var i=0;i<markers.length;i++){//正在分析其他xml元素。。。}}}request.send(null);
希望这会有所帮助。