Learn php curl easily, php curl tutorial and simple example, use php curl with header and data fields easily, easy example to learn php curl.

How To Learn PHP cURL

With our example, you will learn curl easily, I made simple function Qassim_HTTP() to use curl easily and without complexity.

Why Learn PHP cURL

You will need to cURL to make API call, for example get your recent tweets from Twitter API using cURL.

Qassim_HTTP() Function

We will use Qassim_HTTP() function to make API call, with our example, we will get Instagram access token easily.

Get Instagram Access Token Using Qassim_HTTP() Function

Go to Instagram Developer page, and make new client and set REDIRECT URI.

instagram api using php curl

Now in your localhost, make new folder “instagram” and inside it add “index.php” file.

Open your “index.php” file and add Qassim_HTTP() Function:

 /* Function By Qassim Hassan, wp-time.com */
function Qassim_HTTP($method, $url, $header, $data){
    if( $method == 1 ){
        $method_type = 1; // 1 = POST
    }else{
        $method_type = 0; // 0 = GET
    }

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    if( $header !== 0 ){
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    }
    curl_setopt($curl, CURLOPT_POST, $method_type);

    if( $data !== 0 ){
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    $response = curl_exec($curl);
    $json = json_decode($response, true);
    curl_close($curl);

    return $json;
}

Read Qassim_HTTP() Usage:

$Header = array("YOUR_HEADER"); // Your header array, if you do not have header, set value 0
$Data = array("YOUR_FIELDS"); // Your data fields array, if you do not have data, set value 0
$Method = 1; // Method type: 1 = POST, and 0 = GET.
$Url = "https://api.instagram.com/oauth/access_token"; // Enter API URL.
$result = Qassim_HTTP($Method, $Url, $Header, $Data);
print_r($result); // Get result!

Note: If you want POST Method, set value 1 in $Method, if you want GET Method, set value 0 in $Method.

Anyway using Qassim_HTTP() function, we will make API call to get instagram access token, this is a sample request:

curl -F 'client_id=CLIENT_ID' \
-F 'client_secret=CLIENT_SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=YOUR_REDIRECT_URI' \
-F 'code=YOUR_CODE' \
https://api.instagram.com/oauth/access_token

Get instagram access token using Qassim_HTTP() Function, in “index.php” file, add this code:

$method = 1; // $method = 1, because we want POST method.
$url = "https://api.instagram.com/oauth/access_token";
$header = 0; // $header = 0, because we do not have header.

$data = array(
    "client_id" => "xxxx", // ENTER YOUR client_id.
    "client_secret" => "xxxx", // ENTER YOUR client_secret.
    "grant_type" => "authorization_code", // don't change it.
    "redirect_uri" => "http://localhost/instagram/index.php", // Change it to your redirect_uri.
    "code" => $_GET['code']
);

$Result = Qassim_HTTP($method, $url, $header, $data);
echo $Result['access_token'];

Now open Authorize link in browser:


https://www.instagram.com/oauth/authorize/?client_id=enter_your_client_id&redirect_uri=http://localhost/instagram/index.php&response_type=code

Now you will get access token! Open Authorize link again to get access token again, or save your access token in Session.