Recent Articles
Showing posts with label API. Show all posts
Showing posts with label API. Show all posts

Friday, November 15, 2013

Using Curl to contact various APIs in PHP

Friday, November 15, 2013 - 0 Comments

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…………!!!!!!!!!

Tuesday, September 24, 2013

USING LINKEDIN API TO FETCH RECORDS (FULL PROFILE, CONNECTIONS, ETC)

Tuesday, September 24, 2013 - 0 Comments

Hiii, You all know that to allow the user to connect on Linkedin, Facebook or Gmail, etc on your site, you require an API. So,here in this post I will teach you how to connect and fetch the data from any Linkedin account with the help of linkedin API. This functionality is required to add a “Connect To Linkedin” button on your site to directly make the user visiting your site to directly connect to linkedin .
Through this tutorial, you can fetch connections, profile details, job updates, etc from Linkedin profile after login. In this tutorial I am fetching the full profile details of the person who is logging in but you can fetch details according to your needs by changing scope details from “r_fullprofile” to any other like "r_network". Information about scope is present on Linkedin Developer site
To run this tutorial firstly you need an API KEY and Secret API , which you can get very easily by registering your site/application to linkedin.

CODE BEGINS :

<?php
// Change these
define(‘API_KEY’,      ‘your api key here’                                          );
define(‘API_SECRET’,   ‘your secret key here’                                       );
define(‘REDIRECT_URI’, ‘your redirect url’);              //in our case, redirect it on this page
define(‘SCOPE’,        ‘r_fullprofile r_emailaddress rw_nus’     );
// You’ll probably use a database
session_name(‘linkedin’);
session_start();
//$_SESSION['state']=”India”;
// OAuth 2 Control Flow
if (isset($_GET['error'])) {
// LinkedIn returned an error
print $_GET['error'] . ‘: ‘ . $_GET['error_description'];
exit;
} elseif (isset($_GET['code'])) {
// User authorized your application
// Get token so you can make API calls
getAccessToken();
} else {
if ((empty($_SESSION['expires_at'])) || (time() > $_SESSION['expires_at'])) {
// Token has expired, clear the state
$_SESSION = array();
}
if (empty($_SESSION['access_token'])) {
// Start authorization process
getAuthorizationCode();
}
}
// Congratulations! You have a valid token. Now fetch your profile
$user = fetch(‘GET’, ‘/v1/people/~:(id,firstName,lastName,skills,headline,location,industry,email-address,network)’);
//print “Hello $user->firstName $user->lastName.”;
echo “<pre>”;
print_r($user);
echo “<pre>”;
exit;
function getAuthorizationCode() {
$params = array(‘response_type’ => ‘code’,
‘client_id’ => API_KEY,
‘scope’ => SCOPE,
‘state’ => uniqid(”, true), // unique long string
‘redirect_uri’ => REDIRECT_URI,
);
// Authentication request
$url = ‘https://www.linkedin.com/uas/oauth2/authorization?&#8217; . http_build_query($params);
// Needed to identify request when it returns to us
$_SESSION['state'] = $params['state'];
// Redirect user to authenticate
header(“Location: $url”);
exit;
}
function getAccessToken() {
$params = array(‘grant_type’ => ‘authorization_code’,
‘client_id’ => API_KEY,
‘client_secret’ => API_SECRET,
‘code’ => $_GET['code'],
‘redirect_uri’ => REDIRECT_URI,
);
// Access Token request
$url = ‘https://www.linkedin.com/uas/oauth2/accessToken?&#8217; . http_build_query($params);
// Tell streams to make a POST request
$context = stream_context_create(
array(‘http’ =>
array(‘method’ => ‘POST’,
)
)
);
// Retrieve access token information
$response = file_get_contents($url, false, $context);
// Native PHP object, please
$token = json_decode($response);
// Store access token and expiration time
$_SESSION['access_token'] = $token->access_token; // guard this!
$_SESSION['expires_in']   = $token->expires_in; // relative time (in seconds)
$_SESSION['expires_at']   = time() + $_SESSION['expires_in']; // absolute time
return true;
}
function fetch($method, $resource, $body = ”) {
$params = array(‘oauth2_access_token’ => $_SESSION['access_token'],
‘format’ => ‘json’,
);
// Need to use HTTPS
$url = ‘https://api.linkedin.com&#8217; . $resource . ‘?’ . http_build_query($params);
// Tell streams to make a (GET, POST, PUT, or DELETE) request
$context = stream_context_create(
array(‘http’ =>
array(‘method’ => $method,
)
)
);
$response = file_get_contents($url, false, $context);
// Native PHP object, please
return json_decode($response);
}
When you run this code, Linkedin authorization screen will open as shown below :
linkedin_api

Hope you like this post……..Please Comment....!!!!!!
Originally posted at Mycodestock.

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks