needhelp to post request through curl

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amit_n
Forum Newbie
Posts: 9
Joined: Sat Mar 07, 2009 12:19 am

needhelp to post request through curl

Post by amit_n »

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:

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;
}
}

?>

here is the coed for test_authorize_net.php:

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>
Thanks
Amit
Last edited by amit_n on Mon Oct 25, 2010 12:46 am, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: needhelp to post request through curl

Post by requinix »

Are you going to explain what "not able post the request" means?
amit_n
Forum Newbie
Posts: 9
Joined: Sat Mar 07, 2009 12:19 am

Re: needhelp to post request through curl

Post by amit_n »

I have passed the url, post field parameters & values to curl set output but it is not getting executed.

Curl is really new for me.
I have got this code to integrate authorize.net payment gateway. I have modified it for planetauthorize.net payment gateway.

Please help me with this.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: needhelp to post request through curl

Post by requinix »

What's not getting executed? How do you know this? Have you printed out variables and checked that they have the right values? What output are you getting? What are you expecting to get?

And even if it is just a test account, publishing usernames and passwords on the Internet is not a good idea.
amit_n
Forum Newbie
Posts: 9
Joined: Sat Mar 07, 2009 12:19 am

Re: needhelp to post request through curl

Post by amit_n »

I have changed the account details. See I passing transaction details & url to curl functions, So that it will hit the url and will perform the transaction.
I have checked all the variables by printing. They are accurate. On my WAMP server curl extension is running.
I don't know where i am making the mistake.

Can I pm you the code?



Thanks
Amit
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: needhelp to post request through curl

Post by requinix »

Do you get any kind of response at all from the server? Take a close look at the value of $auth_only_response.
amit_n
Forum Newbie
Posts: 9
Joined: Sat Mar 07, 2009 12:19 am

Re: needhelp to post request through curl

Post by amit_n »

Tasairis,

It is not giving any error. It's working well on hosted site. I think my WAMP server is not properly configured for CURL.

Thanks for all your help.
Amit
Post Reply