Page 1 of 1

Send post data and receive response from https url

Posted: Fri Jan 21, 2011 3:55 am
by maanijanjua007
Hello Everyone,

i am using WAMP on Windows 2003 server, i am having problem in retreiving response from the https url by sending Post data to it using php curl.

i have turned on the mod_ssl module from php.ini for both php and apache.

Here is my code:

Code: Select all


$VPSProtocol = $_REQUEST['VPSProtocol'];
$TxType = $_REQUEST['TxType'];
$Vendor = $_REQUEST['Vendor'];
$VendorTxCode = $_REQUEST['VendorTxCode'];
$Description = $_REQUEST['Description'];			
$Amount = $_REQUEST['Amount'];
$Currency = $_REQUEST['Currency'];
$CardHolder = $_REQUEST['CardHolder'];
$CardNumber = $_REQUEST['CardNumber'];
$ExpiryDate = $_REQUEST['ExpiryDate'];
$CardType = $_REQUEST['CardType'];
$CV2 = $_REQUEST['CV2'];
$BillingSurname = $_REQUEST['BillingSurname'];
$BillingFirstname = $_REQUEST['BillingFirstnames'];
$BillingAddress = $_REQUEST['BillingAddress1'];
$BillingCity = $_REQUEST['BillingCity'];
$BillingPostCode = $_REQUEST['BillingPostCode'];
$BillingCountry = $_REQUEST['BillingCountry'];
$BillingState = $_REQUEST['BillingState'];
$DeliverySurname = $_REQUEST['DeliverySurname'];
$DeliveryFirstname = $_REQUEST['DeliveryFirstnames'];
$DeliveryAddress = $_REQUEST['DeliveryAddress1'];
$DeliveryCity = $_REQUEST['DeliveryCity'];
$DeliveryPostCode = $_REQUEST['DeliveryPostCode'];
$DeliveryCountry = $_REQUEST['DeliveryCountry'];
$DeliveryState = $_REQUEST['DeliveryState'];

//set POST variables
$url = 'https://test.sagepay.com/gateway/service/vspdirect-register.vsp?';
$fields = array(
            'VPSProtocol'=>urlencode($VPSProtocol),
            'TxType'=>urlencode($TxType),
            'Vendor'=>urlencode($Vendor),
            'VendorTxCode'=>urlencode($VendorTxCode),
            'Description'=>urlencode($Description),
            'Amount'=>urlencode($Amount),
            'Currency'=>urlencode($Currency),
			'CardHolder'=>urlencode($CardHolder),
            'CardNumber'=>urlencode($CardNumber),
            'ExpiryDate'=>urlencode($ExpiryDate),
            'CardType'=>urlencode($CardType),
            'CV2'=>urlencode($CV2),
            'BillingSurname'=>urlencode($BillingSurname),
			'BillingFirstnames'=>urlencode($BillingFirstname),
            'BillingAddress1'=>urlencode($BillingAddress),
            'BillingCity'=>urlencode($BillingCity),
            'BillingPostCode'=>urlencode($BillingPostCode),
            'BillingCountry'=>urlencode($BillingCountry),
            'BillingState'=>urlencode($BillingState),
			'DeliverySurname'=>urlencode($DeliverySurname),
            'DeliveryFirstnames'=>urlencode($DeliveryFirstname),
            'DeliveryAddress1'=>urlencode($DeliveryAddress),
            'DeliveryCity'=>urlencode($DeliveryCity),
            'DeliveryPostCode'=>urlencode($DeliveryPostCode),
            'DeliveryCountry'=>urlencode($DeliveryCountry),
			'DeliveryState'=>urlencode($DeliveryState)
        );


foreach($fields as $key=>$value) { 
	$fields_string .= $key.'='.$value.'&'; 
}
rtrim($fields_string,'&');

$curl = curl_init(); 

$header[] = "Content-Type: application/x-www-form-URLEncoded";

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ; 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_SSLVERSION,3); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($curl, CURLOPT_HEADER, true); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 

$data = curl_exec($curl); 
echo $data;
curl_close($curl);

What seems wrong, any positive and quick response will be highly appreciated.

Many Thanks in advance
maani

Re: SEND POST DATA AND RECEIVE RESPONSE FROM HTTPS URL

Posted: Fri Jan 21, 2011 2:44 pm
by John Cartwright
You are setting some headers (HTTP_AUTH) which may conflict with the request. Try simply:

Code: Select all

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$data = curl_exec($curl); 

Re: SEND POST DATA AND RECEIVE RESPONSE FROM HTTPS URL

Posted: Fri Jan 21, 2011 2:52 pm
by Benjamin
8O Are the caps necessary?

Re: SEND POST DATA AND RECEIVE RESPONSE FROM HTTPS URL

Posted: Fri Jan 21, 2011 2:56 pm
by John Cartwright
Benjamin wrote:8O Are the caps necessary?
I can't believe I didn't realize that (I usually skip such threads ;)). Bad OP! :)

Re: Send post data and receive response from https url

Posted: Mon Jan 24, 2011 12:54 am
by maanijanjua007
thanks for the response, i will try it simple and will let you know about the result.

Regards
maani