PHP cURL Basic CRUD Example

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.

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();

curl_init();    
curl_setopt();  
curl_exec();      
curl_close();     

PHP cURL POST Request

 '[email protected]',
    'passcode' => 'Tamile@rokers'
);

$ch_curl = curl_init('https://domainname.uk/api');
curl_setopt($ch_curl, CURLOPT_POSTFIELDS, $par5amenters);
curl_setopt($ch_curl, CURLOPT_RETURNTRANSFER, true);

$results = curl_exec($ch_curl);
curl_close($ch_curl);

$data_results - json_decode($results);
print_r($data_results);

PHP cURL GET Request


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);

    curl_close($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


Web Programming Tutorials Example with Demo

Read :

Summary

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 infinityknow.com 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.

Leave a Comment