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

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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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

<?php

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

$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

$ch_curl = curl_init();

curl_setopt($ch_curl, CURLOPT_URL, 'https://domainname.uk/products-list');
curl_setopt($ch_curl, CURLOPT_RETURNTRANSFER, true);

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

$productList_array = json_decode($productList);
print_r($productList_array);

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

<?php
    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/admin.uk/");

        curl_setopt($ch_curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)'); 
        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); 
        } 
        else 
        { 
            print_r(curl_getinfo($ch_curl)); 
        }

        curl_close($ch_curl);
    }

?>

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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Create Dynamic Image Fancybox Gallery with jQuery, PHP & MySQL
  2.   PHP cURL HTTP Request (GET & POST)
  3.   PHP cURL Set custom header Authorization
  4.   PHP cURL Http Authorization Pass Header Examples
  5.   PHP Curl Fetch getAllheaders HTTP Request
  6.   Laravel Pivot Tables Tutorial Example
  7.   Get Unique Collection values(Remove Duplicates from Collection) using Laravel
  8.   Laravel 6 JQuery Ajax Post Request Example
  9.   PHP Curl POST and GET Methods
  10.   Vuejs autocomplete search with typeahead js