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.

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.

Leave a Comment