PHP cURL HTTP Request (GET & POST)

Today, We want to share with you PHP cURL HTTP Request (GET & POST).In this post we will show you PHP cURL GET request and request’s body, hear for Execute a HTTP POST Using PHP CURL we will give you demo and example for implement.In this post, we will learn about cURL API calls with PHP and json data (GET – POST – PUT – DELETE) with an example.

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

PHP cURL HTTP Request (GET & POST)

There are the Following The simple About PHP cURL HTTP Request (GET & POST) Full Information With Example and source code.

As I will cover this Post with live Working example to develop How To POST JSON Data with PHP cURL, so the How To Make cURL HTTP Request for this example is following below.

What is cURL in PHP?

CURL- Client URL Library

CURL different serval types of servers as wlle as some server side and client side request communicates with different types of good response protocols Like as a http, https as well as ftp.

Make PHP cURL HTTP GET Request

$chCurl_init = curl_init();

curl_setopt_array($chCurl_init, array(
    CURLOPT_URL => "https://www.pakainfo.com",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_TIMEOUT => 30000,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
    	// Set Here Your Requesred Headers
        'Content-Type: application/json',
    ),
));
$results = curl_exec($chCurl_init);
$get_err = curl_error($chCurl_init);
curl_close($chCurl_init);

if ($get_err) {
    echo "chCurl_init Error #:" . $get_err;
} else {
    print_r(json_decode($results));
}

Make PHP cURL HTTP POST Request

// Create POST fields array
$userFields = [
    'username' => 'jdname',
    'age' => '25',
    'lastname' => 'gondaliya',
    'firstname' => 'jaydeep',
];

$chCurl_init = curl_init();

curl_setopt_array($chCurl_init, array(
    CURLOPT_URL => "https://www.pakainfo.com",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30000,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode($userFields),
    CURLOPT_HTTPHEADER => array(
    	// here Set your security here requred headers
        "accept: */*",
        "accept-language: en-US,en;q=0.8",
        "content-type: application/json",
    ),
));

$results = curl_exec($chCurl_init);
$get_err = curl_error($chCurl_init);

curl_close($chCurl_init);

if ($get_err) {
    echo "your Project chCurl_init Error #:" . $get_err;
} else {
    print_r(json_decode($results));
}

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

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

I hope you get an idea about PHP cURL HTTP Request (GET & POST).
I would like to have feedback on my Pakainfo.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.   PHP cURL Set custom header Authorization
  2.   How to return view with flash message in Laravel 6?
  3.   Check if Object is empty in VueJS
  4.   PHP Curl POST and GET Methods
  5.   WordPress Force SSL HTTPS On All Pages
  6.   PHP Laravel CURL HTTP GET POST Request
  7.   Laravel 5.8 active menu item using request
  8.   PHP Set custom headers using cURL Example
  9.   Vue 2.0 Hello World Step By Step
  10.   Increment Decrement with Laravel 6 Example

Leave a Reply

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