Friday, November 15, 2013

Using Curl to contact various APIs in PHP

Hiiiii, being a developer if you want to contact or request APIs or you have to request payment gateway for sending the amount deduction request to payment sites then you can contact them with the help of form submissions or using web services . Most of us don't know how to use web services and to use which web services for your work. so, here in this post I'll give a tutorial of using a web services.
Web services are used for connection between two areas of data(sites) over network. For this tutorial, I will be using "curl" web services to connect to various APIs. Curl uses HTML, javascript and high level languages like "PHP",etc to perform its task of connection between various sites. Using curl we can send a data in the form of XML or any other format which your requesting APIs demands. 


curl-web-service

CODE BEGINS :

<?php
  $url = 'https://www.example.com/receiver'; 

            $fields = array(
                'ssl_merchant_id'=>'your user id',
                'ssl_user_id'=>'your user id',
                'ssl_pin'=>'your user pin',
'ssl_test_mode'=>'false',  
                'ssl_error_url'=>'http://example.com/unsuccess',
                'ssl_receipt_apprvl_get_url'=>'http://example.com/success',
                'ssl_transaction_type'=>'ccsale',
                'ssl_billing_cycle'=>'ANNUALLY',
                'ssl_next_payment_date'=>'11/14/2013',              
                'ssl_amount'=>'45',
                'ssl_card_number'=>'your credit card number',
                'ssl_exp_date'=>'1214',
                'ssl_cvv2cvc2_indicator'=>'1',                
                'ssl_cvv2cvc2'=>'your card cvv number',
                'ssl_first_name'=>'XYZ', 
                'ssl_last_name'=>'ABC', 
                'ssl_email'=>'test@test.com',
                'ssl_avs_zip'=>'zip code',   
                'ssl_invoice_number'=>urlencode(rand()),
            );  
    //initialize the post string variable
    $fields_string = '';
    //build the post string
    foreach($fields as $key=>$value) { $fields_string .=$key.'='.$value.'&'; }
    rtrim($fields_string, "&");
    //open curl session
    $ch = curl_init();
    //begin seting curl options
    //set URL
    curl_setopt($ch, CURLOPT_URL, $url);
    //set method
    curl_setopt($ch, CURLOPT_POST, 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);
    //perform the curl post and store the result
    $result = curl_exec($ch);
    //close the curl session
    curl_close($ch);   
    //a nice message to prevent people from seeing a blank screen
    echo "Processing, please wait..."   
?>

Hope you like this post……..Please Comment…………!!!!!!!!!


Tags: ,

0 Responses to “Using Curl to contact various APIs in PHP”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks