PHP cURL Basic CRUD Example

Laravel Redirect To Another URL / Web Page - HTTP Laravel Redirects

Today, We want to share with you PHP cURL Basic CRUD Example.In this post we will show you php curl get,put,post,delete request with headers, hear for What is cURL and how to POST Form Data and file with cURL with Example we will give you demo and example for implement.In this post, we will learn about php curl set header authorization with an example.

PHP cURL Basic CRUD Example

There are the Following The simple About cURL API calls with PHP and json data (GET POST PUT DELETE) Full Information With Example and source code.

Read Also:  PHP cURL Http Authorization Pass Header Examples

As I will cover this Post with live Working example to develop php curl get request with parameters, so the how to fetch data using curl in php is used for this example is following below.

PHP cURL Basics

curl_init(); initializes a cURL session

curl_setopt(); changes the cURL session behavior with options

curl_exec(); executes the started cURL session

curl_close(); closes the cURL session and deletes the variable made by curl_init();




$par5amenters = array(
    'email' => '[email protected]',
    'passcode' => '[email protected]'

$ch_curl = curl_init('');
curl_setopt($ch_curl, CURLOPT_POSTFIELDS, $par5amenters);
curl_setopt($ch_curl, CURLOPT_RETURNTRANSFER, true);

$results = curl_exec($ch_curl);

$data_results - json_decode($results);

PHP cURL GET Request


$ch_curl = curl_init();

curl_setopt($ch_curl, CURLOPT_URL, '');
curl_setopt($ch_curl, CURLOPT_RETURNTRANSFER, true);

$productList = curl_exec($ch_curl);

$productList_array = json_decode($productList);

PHP cURL Header

curl_setopt($ch_curl, CURLOPT_HTTPHEADER, array(
    'Header-Key-first: authrization-Value-data',
    'Header-Key-second: authrization-Value-data-2'

Executing Curl with PHP

function callService($api_uri, $data, $request_header) {
    $curl_post_data = json_encode($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_uri);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $request_header);

    $results = curl_exec($ch);


    $data_results = json_decode($results, true);

    if (!is_array($data_results)) {
        $data_results = [
            "error" => [
                "code"      => 0,
                "message"   => "Error In CURL request. Fail to parse curl results.",
                'response'  => $results,
                'url'       => $api_uri,
                'arguments' => $data,
                'header'    => $request_header,
    return $data_results;

Example 2: php curl get request with parameters

    function cURL() {
        $ch_curl = curl_init(); 

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

        curl_setopt($ch_curl, CURLOPT_URL, "https://your_web_api/");

        curl_setopt($ch_curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+'); 
        curl_setopt($ch_curl, CURLOPT_FOLLOWLOCATION, true); 
        curl_setopt($ch_curl, CURLOPT_FAILONERROR, true); 
        curl_setopt($ch_curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch_curl, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch_curl, CURLOPT_TIMEOUT, 50);

        curl_setopt($ch_curl, CURLOPT_SSL_VERIFYHOST, false); 
        curl_setopt($ch_curl, CURLOPT_SSL_VERIFYPEER, false); 

        $html = curl_exec($ch_curl); 

        if (curl_errno($ch_curl)) 
            echo 'Some cURL error: ' . curl_error($ch_curl); 



Web Programming Tutorials Example with Demo

Read :

Read Also:  WooCommerce Get Product Taxonomies


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How to use cURL with PHP – Basic Example.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

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