简体   繁体   中英

Curl request response not getting

I am sending payment info to Virtual merchant payment gateway for payment system using curl. This is my code :

$Url= "https://www.myvirtualmerchant.com/VirtualMerchant/process.do";
    // is cURL installed yet?
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }

    // OK cool - then let's create a new cURL resource handle
    $ch = curl_init();

    // Now set some options (most are optional)

    // Set URL to download
    curl_setopt($ch, CURLOPT_URL, $Url);


    // Include header in result? (0 = yes, 1 = no)
   // curl_setopt($ch, CURLOPT_HEADER, 0);

    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);


    $fields = array(
            'ssl_card_number'=>urlencode($_POST['ssl_card_number']),
            'ssl_exp_date'=>urlencode($_POST['ssl_exp_date']),
            'ssl_cvv2cvc2'=>urlencode($_POST['ssl_cvv2cvc2']),
            'ssl_avs_address'=>urlencode($_POST['ssl_avs_address']),
            'ssl_avs_zip'=>urlencode($_POST['ssl_avs_zip']),
            'ssl_merchant_id'=>urlencode($_POST['ssl_merchant_id']),
            'ssl_user_id'=>urlencode($_POST['ssl_user_id']),
            'ssl_pin'=>urlencode($_POST['ssl_pin']),
            'ssl_transaction_type'=>urlencode($_POST['ssl_transaction_type']),
            'ssl_amount'=>urlencode($_POST['ssl_amount']),
            'ssl_show_form'=>urlencode($_POST['ssl_show_form']),
            'TransactionType'=>urlencode($_POST['TransactionType'])
        );

    //url-ify the data for the POST
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string,'&');
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

    // Download the given URL, and return output
    echo $output = curl_exec($ch);

    // Close the cURL resource, and free system resources
    curl_close($ch);

    print_r($output); 

But in $output i am getting nothing, not any error or message. Am i doing it wrong ? please tell me ?

First I'd check your mechant_id, pin, etc. Below is working code I created after working through a similar problem.

<html>
    <body>
        <p>--start--</p>

<?php
//if you have a live account don't use the "demo" post url it won't work
$post_url = 'https://www.myvirtualmerchant.com/VirtualMerchant/process.do';

//replace the xxx's with your proper merchant_id, etc. 
//they will give you these when you activate your account

//I've set form to not show, and ssl_result_format =>ascii to get a string returned

$fields = array(
    'ssl_merchant_id'           =>'xxxxxx',
    'ssl_user_id'               =>'xxx',
    'ssl_pin'               =>'xxxxx',
    'ssl_show_form'             =>'false',
    'ssl_result_format'         =>'ascii',
    'ssl_test_mode'             =>'false',
    'ssl_transaction_type'      =>'ccsale',
    'ssl_amount'                =>'1.44',
    'ssl_card_number'           =>'5000300020003003',
     'ssl_exp_date'             =>'1214',
    'ssl_avs_address'           =>'Test 3',
    'ssl_avs_zip'               =>'123456',
    'ssl_cvv2cvc2'              =>'123',
);

//build the post string
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .=$key.'='.$value.'&'; }
rtrim($fields_string, "&");

//open curl session
// documentation on curl options at http://www.php.net/curl_setopt

$ch = curl_init();
//begin seting curl options
//set URL
curl_setopt($ch, CURLOPT_URL, $post_url);

//set method
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//set post data string
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

//these two options are frequently necessary to avoid SSL errors with PHP
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$result = curl_exec($ch); 

if($result === FALSE) {
    //post failed
    die(curl_error($ch));

} else {
    //got a response
    //some people seem to get name/value pairs delimited with "&" 
    //but currently mine is \n 
    //support told me there's no way to change it..
    $response_array = explode("\n",$result); 

    //make it nice and useful
    foreach( $response_array as $k=>$v ){
        $v=explode("=",$v);
        $a[$v[0]]=$v[1];
    }

    //show the whole array
    print_r($a);

    //use a specific return value
    //returns "APPROVAL" if it went through
    echo('<h1>'. $a[ssl_result_message] . '</h1>');
}
?>

       <p>--end--</p>
    </body>
</html>

The code above should net you a screen like this:

--start--

Array ( [ssl_card_number] => 50**********3003 [ssl_exp_date] => 1214 [ssl_amount] => 1.44 [ssl_customer_code] => [ssl_salestax] => [ssl_invoice_number] => [ssl_description] => [ssl_departure_date] => [ssl_completion_date] => [ssl_company] => [ssl_first_name] => [ssl_last_name] => [ssl_avs_address] => Test 3 [ssl_address2] => [ssl_city] => [ssl_state] => [ssl_avs_zip] => 123456 [ssl_country] => [ssl_phone] => [ssl_email] => [ssl_result] => 0 [ssl_result_message] => APPROVAL [ssl_txn_id] => AA49315-1234567-F78F-468F-AF1A-F5C4ADCFFB1E [ssl_approval_code] => N53032 [ssl_cvv2_response] => [ssl_avs_response] => [ssl_account_balance] => 0.00 [ssl_txn_time] => 01/15/2014 11:53:15 AM )

APPROVAL

--end--

Make sure you delete all these test purchases when you finished testing. I'm told they will inhibit your real purchases from posting.

try this to find out the error

var_dump(curl_error($ch));

before and calling curl_exec($ch);

You are calling an HTTPS page. Please refer to following link

http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

Try to find error give this code before curl close:--

echo "Curl Error :--" . curl_error($ch);

if no error found do like this:-

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch); 

then

print_r($result);
exit;

Try this:

$output = curl_exec($ch);
$response = curl_getinfo($ch);
echo "<pre>";
print_r($response);
echo "</pre>";

Hope you get the response :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM