needhelp to post request through curl
Posted: Mon Oct 25, 2010 12:04 am
Hi, I am trying to post the request to payment gateway url using curl lib.
please help me with following code. I am not able post the request.
here is the coed for authorize_net_request.php:
here is the coed for test_authorize_net.php:
Thanks
Amit
please help me with following code. I am not able post the request.
here is the coed for authorize_net_request.php:
Code: Select all
<?php
class PLANETAuthorizeNetRequest
{
// PLANETAuthorize Server URL
private $_mUrl;
// Will hold the current request to be sent to PLANETAuthorize.net
private $_mRequest;
// Constructor initializes the class with URL of PLANETAuthorize.net
public function __construct($url)
{
// PLANETAuthorize.net URL
$this->_mUrl = $url;
}
public function SetRequest($request)
{
$this->_mRequest = '';
$request_init = array ('username' => '*******',
'password' => '******',
'x_delim_data' => 'TRUE',
'x_delim_char' => '|',
'x_relay_response' => 'FALSE');
$request = array_merge($request_init, $request);
foreach($request as $key => $value )
$this->_mRequest .= $key . '=' . urlencode($value) . '&';
return $this->_mRequest;
}
// Send an HTTP POST request to PLANETAuthorize.net using cURL
public function GetResponse()
{
// Initialize a cURL session
$ch = curl_init();
// Prepare for an HTTP POST request
curl_setopt($ch, CURLOPT_POST, 1);
// Prepare the request to be POSTed
curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim($this->_mRequest, '& '));
// Set the URL where we want to POST our data
curl_setopt($ch, CURLOPT_URL, $this->_mUrl);
/* Do not verify the Common name of the peer certificate in the SSL
handshake */
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// Prevent cURL from verifying the peer's certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
/* We want cURL to directly return the transfer instead of
printing it */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Perform a cURL session
$result = curl_exec($ch);
// Close a cURL session
curl_close ($ch);
// Return the response
return $result;
}
}
?>
Code: Select all
<?php
session_start();
if (empty ($_GET['step']))
{
require_once 'authorize_net_request.php';
$myUrl = "https://urlgoes here";
$request = new PLANETAuthorizeNetRequest($myUrl);
// Auth
$transaction =array (
'type' => 'sale',
'orderid' => '110',
'ccnumber' => '4111111111111111',
'cvv' => '9999',
'ccexp' => '1201',
'cctype' => 'American Express',
'amount' => '24.00',
'firstname' => 'Andy,
'lastname' => 'N,
'email' => 'andy.n@xyz.net',
'ipaddress' => '',
'address1' => 'asdhfasd jkfa jdf',
'address2' => 'asgdv fg hasdf',
'city' => 'Pune',
'state' => 'Maharashtra',
'zip' => '411037',
'country' => 'India'); // Transaction type
$request->SetRequest($transaction);
$auth_only_response = $request->GetResponse();
$_SESSION['auth_only_response'] = $auth_only_response;
$auth_only_response = explode('|', $auth_only_response);
// Read the transaction ID, which will be necessary for taking the payment
$response = $auth_only_response[0];
$responsetext = $auth_only_response[1];
$authcode = $auth_only_response[2];
$ref_trans_id = $auth_only_response[3];
$cvvresponse = $auth_only_response[5];
$response_code = $auth_only_response[7];
// Capture
$transaction = array ('transactionid' =>$ref_trans_id, // Transaction id
'type' => 'capture',
'username' => '*****',
'password' => '****',
'amount' => '24.00',
'orderid' => '110'
); // Transaction type
$request->SetRequest($transaction);
$prior_auth_capture_response = $request->GetResponse();
$_SESSION['prior_auth_capture_response'] = $prior_auth_capture_response;
}
else
{
switch ($_GET['step'])
{
case 1:
print $_SESSION['auth_only_response'];
break;
case 2:
print $_SESSION['prior_auth_capture_response'];
break;
}
exit();
}
?>
<frameset cols="50%, 50%">
<frame src="test_authorize_net.php?step=1" />
<frame src="test_authorize_net.php?step=2" />
</frameset><noframes></noframes>
Amit