Skip to main content

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.

Also Read :  Form Submission With Validation Using Vue.JS and PHP

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://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://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 :

Also Read :  authentication login with username or email in laravel

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.

Web Developer

Hi, I’m Web developer, PHP developer.Pakainfo.com is the most popular Programming & Web Development blog. Our mission is to provide the best online resources on programming and web development.Any visitors of this site are free to browse our tutorials, live demos, Examples and download scripts.If you have a project that you want to get started, launch quickly, contact me. Email : [email protected]