In my website, I am submitting a html form to a payment site (webmasterchecks.com) and the site responses me by XML response. The XML response is displaying on external link (https://api.webmasterchecks.com/payments/add) after normal form submission(by clicking on submit button). I want to read the XML response and update my database. My coding language is PHP.
HTML form:
<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>
I tried to submit the form using jquery post and get the json data. But The form is not submitting (not getting any error in console as well). Code is given below.
$(function(){
$("form[name=payment_form]").submit(function(){
$.post($(this).attr("action"), $(this).serialize(), function(jsonData){
alert(jsonData);
}, "json");
return false;
});
});
I also tried to use curl, but did not succeed. Its displaying the API key is invalid where as using the above html form, i am getting response. CURL code is given below.
$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();
How can i update my database using the XML response?
Thanks in advance.
EDIT
My api key contains few plus “+” sign on it. Sample format of API key is given below.
q5Un5ObLZs2ovY2COW+LvHzEVdUy0kR3u6dPwh/p+wzlbh80vONBbo+otLpBwPqnvP/VjhglfFos51sLFDpUHi+6GnVbLtR3ATjSz9trGoKLFgrK/ostPUG4t9XV1EdS10JzVZFscIxUu2LVmJN9NVpCgaD9NaA
What you will need to do here is send your data through to a local script and then package it up into a curl request to interact with the remote server via its API.
Note I have removed client_id and api_key from the form as these should never be shown to the public like this
local_script.php
This is how I would do it, as I am versed in DOMDocument for parsing and writing xml,you can either parse using the manual for DOMDocument http://www.php.net/manual/en/class.domdocument.php or use SIMPLEXML http://php.net/manual/en/book.simplexml.php either way you need to parse the xml to add the data to your db.