PHP cUrl GET & POST Methods Example

PHP cUrl GET & POST Methods Example

Today, We want to share with you PHP cUrl GET & POST Methods Example.
In this post we will show you PHP CURL POST & GET Examples – Submit Form using PHP CURL, hear for PHP + curl, HTTP POST sample code we will give you demo and example for implement.
In this post, we will learn about Post data using cUrl in PHP Example with an example.

Also Read This πŸ‘‰   Export Data to Excel in Laravel Example

Post data using cUrl in PHP Example

what is cURL?

The cURL Simple Means is a library(libs) that lets you create HTTP requests in PHP server.

“The cURL one type of library (or, ‘libcurl’ which is the manage package name on the server side) is every time used in Core PHP or oop any framework used to access all data from outside onther web pages.(in json format) … libcurl(libs) currently supports Likw as a http used, https used, ftp used, gopher used, telnet used, dict used, file used, and ldap used protocols.”

//API Url
$url = '';

//Initiate cURL.
$ch = curl_init($url);

//The simple JSON data.
$jsonData = array(
    'username' => '[email protected]',
    'password' => 'n*********'

//here Encode the all array into JSON data.
$jsonDataEncoded = json_encode($jsonData);

//here cURL that we want to request send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);


//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

//Execute the request

// close cURL resource, and free up system resources
$obj = json_decode($result, TRUE);

Object to get Data using PHP with curl

$obj_data = json_decode($result_data, TRUE);
echo $access_token;

How to Send HTTPS posts using php

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //used  On dev server only!
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//other way

Sendipakainfo auth in headers php curl

$header   = array();
$header[] = 'Authorization: Bearer ' . $access_token;
$header[] = 'Content-Type:  application/json ';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDatapay);

Sendipakainfo a username and password with PHP CURL

$username="[email protected]";

//init curl(inialization)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_name);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//username and password pass data 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

//Basic or simple Authorization

//execute curl
$output_data = curl_exec($ch);

//info data using php curl
$info = curl_getinfo($ch);

//close the cURL in php

Access-Control-Allow-Origin to header in PHP with curl

header('Content-Type: image/jpeg');
header("Access-Control-Allow-Origin: *");