Jump to content

Recommended Posts

Hi

 

I am well versed in VB but and am starting to learn PHP.

 

I am about to start using a new payment gateway for taking orders in my cubecart shopping cart.  Setting it up in cubecart is very simple but I also have an offline order management system that I created for taking orders over the phone or in my shop.

 

The new gateway have an API which wil accept information via XML.  However I cant post XML directly from my application as the gateway will only accept XML from a php connection (thats what the gateway say).

 

If i put the following information in an XML file (api_transaction.xml)on my server

<?xml version="1.0"?>
<request>
<type>transaction</type>
<authtype>authorise</authtype>
<authentication>
	<shreference>SH215940</shreference>
	<checkcode>441947</checkcode>
</authentication>
<transaction>
	<cardnumber>4242424242424242</cardnumber>
	<cardstartmonth>01</cardstartmonth>
	<cardstartyear>04</cardstartyear>
	<cardexpiremonth>01</cardexpiremonth>
	<cardexpireyear>06</cardexpireyear>
	<cv2>424</cv2>
	<switchnumber>2</switchnumber>
	<cardholdersname>John Doe</cardholdersname>
	<cardholdersemail>[email protected]</cardholdersemail>
	<cardholderaddr1>Address 1</cardholderaddr1>
	<cardholdercity>City</cardholdercity>
	<cardholderstate>State</cardholderstate>
	<cardholderpostcode>Postcode</cardholderpostcode>
	<currency>GBP</currency>
	<transactionamount>34.99</transactionamount>
	<transactiontax>3.50</transactiontax>
	<shippingcharge>1.00</shippingcharge>
	</transaction>
</request>

Then I can use the following PHP script to process the file

<?php
$xmlfile = "./api_transaction.xml";

$xmldoc = file_get_contents($xmlfile);

$post_vars = "xmldoc=".urlencode($xmldoc);
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, "https://www.secure-server-hosting.com/secutran/api.php");
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_vars);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_TIMEOUT, 15);
$xml_text = trim(curl_exec ($ch));
if ($xml_text == "") print (date("Y-m-d H:i:s")." XML Doc Empty");
if (curl_error($ch)) print (date("Y-m-d H:i:s")." cURL Call Failed - ".curl_error($ch));
curl_close ($ch);

print $xml_text;
?>

 

My question is this.  Is there a way that I can POST my data to the php script and have it reformat it and then re-post it to their server.

Much like the above script without the need for the XML file on my server.  The only way I can do it at the moment is to have my application FTP the file to my server before loading the above script in a web browser.  For obvious reasons I would like to stay clear of this as the file contains credit card details.

The above scripts contain links to my sandbox account so dont worry about any of the information contained in them.

 

I would appreciate it if someone could help me out.

 

Gaz

Link to comment
https://forums.phpfreaks.com/topic/183309-need-to-re-post-data/
Share on other sites

Yes. You can create a regular html form and then in php access the posted data via the $_POST array

 

// example to see posted data. echo out all of the data 
echo "<pre>";
print_r($_POST);

 

You can then reformat to match what would be in the xml file, most likely by just having a string with the format, but using the posted array info in place of the values (like $_POST['name'] or whatever).  That string would be the value of that $xmldoc, instead of using file_get_contents.  Then you would have the rest of that script (the curl stuff) execute as normal.

Link to comment
https://forums.phpfreaks.com/topic/183309-need-to-re-post-data/#findComment-967596
Share on other sites

Thanks for the response, I didnt explain what I wanted very well.

 

With the stand alone application I have created I can send an xml string to a php script, I just need the script to receive the post and then re-post it to the gateway server.

 

How do i make the original php script (See below) read the POST and then process it directly instead of reading from the external file?

<?php
$xmlfile = "./api_transaction.xml";
   
$xmldoc = file_get_contents($xmlfile);
   
$post_vars = "xmldoc=".urlencode($xmldoc);
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, "https://www.secure-server-hosting.com/secutran/api.php");
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_vars);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_TIMEOUT, 15);
$xml_text = trim(curl_exec ($ch));
if ($xml_text == "") print (date("Y-m-d H:i:s")." XML Doc Empty");
if (curl_error($ch)) print (date("Y-m-d H:i:s")." cURL Call Failed - ".curl_error($ch));
curl_close ($ch);

print $xml_text;
?>

Link to comment
https://forums.phpfreaks.com/topic/183309-need-to-re-post-data/#findComment-967602
Share on other sites

Okay maybe I'm missing something, but you would do what I said in previous post:  Posted variables are in the $_POST array.  In your script, instead of using file_get_contents, you would grab the data from the $_POST array, and put it into a string with the xml formatting, except use the $_POST elements instead of hardcoded data.  Example:

 

<?php
$xmlfile = "./api_transaction.xml";

  
$xmldoc = <<<POSTINFO
<name>{$_POST['name']}</name>
<address>{$_POST['address']}</address>
POSTINFO;
   
$post_vars = "xmldoc=".urlencode($xmldoc);
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, "https://www.secure-server-hosting.com/secutran/api.php");
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_vars);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_TIMEOUT, 15);
$xml_text = trim(curl_exec ($ch));
if ($xml_text == "") print (date("Y-m-d H:i:s")." XML Doc Empty");
if (curl_error($ch)) print (date("Y-m-d H:i:s")." cURL Call Failed - ".curl_error($ch));
curl_close ($ch);

print $xml_text;
?>

 

or if you already have your standalone application posting the entire xml string to your script (let's call the posted var 'xmlData') you would simply do

 

$xmldoc = $_POST['xmlData'];

 

instead of

 

$xmldoc = file_get_contents($xmlfile);

Link to comment
https://forums.phpfreaks.com/topic/183309-need-to-re-post-data/#findComment-967616
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.