How to use curl in php

cURL is a php library and stands for the client Url, with the help of cURL we can send files and download data over http and ftp requests. cURL also has a command line tool so we can also in command line.

cURL is very helpful for calling rest full API’s. In API, we can set post params, headers, cookies, and so we can set header token with SSL connection.

Let see how can we use cURL

Before this, we need to check cURL should be enable.

// check cURL is here
print_r(get_loaded_extensions());

     // create a new cURL resource  
    $curl = curl_init();

    if (!$curl) {
        die("Couldn't initialize a cURL handle");
    }

    // Set the file URL to fetch through cURL
    curl_setopt($curl, CURLOPT_URL, "https://phpguruji.com/");

    // Set a different user agent string
    curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');

    // Follow redirects, if any
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

    // Fail the cURL request if response code = 400 (like 404 errors)
    curl_setopt($curl, CURLOPT_FAILONERROR, true);

    // Return the actual result of the curl result instead of success code
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    // execute time to wait for 10 seconds to connect, set 0 to wait indefinitely
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);

    // set request timeout for a maximum of 50 seconds
    curl_setopt($curl, CURLOPT_TIMEOUT, 50);

    // check the SSL certificates off
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    // Now execute our url and hold return html in $html
    $html = curl_exec($curl);

    // Check if any error has occurred
    if (curl_errno($curl))
    {
        echo 'cURL error: ' . curl_error($curl);
    }
    else
    {
        // cURL successfully executed
        print_r(curl_getinfo($curl));
    }

    // close destroy cURL resource from memory
    curl_close($curl);
 
// cURL with Post request and headers

function curlPost($url, $data=NULL, $headers = NULL) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    if(!empty($data)){
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }

    if (!empty($headers)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }

    $res = curl_exec($ch);

    if (curl_error($ch)) {
        trigger_error('Curl Error:' . curl_error($ch));
    }

    curl_close($ch);
    return $res;
}

$post_data = [
		'username' => 'admin',
		'password' => '12345',
	    ];

$token  = 'asdasdasdasdsadas'; //token
$headers  = [
		'Content-Type: application/json', // for define content type that is json
		'bearer: '.$token, // send token in header request
		'Content-length: 100' // content length for example 100 characters 
	    ];

curlPost('https://phpguruji.com', $post_data, $headers); // call function

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow by Email
LinkedIn
Share
Instagram