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.

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.

  How to create category menu list using css and JQuery?

“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 = 'http://api.pakainfo.devz.no/v1/login';

//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);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

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

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

Object to get Data using PHP with curl

$result_data=curl_exec($ch);
$obj_data = json_decode($result_data, TRUE);
$access_token=$obj_data['token'];
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

<?php
$username="[email protected]";
$password="n*********";

//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
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

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

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

//close the cURL in php
curl_close($ch);
?>

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

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

>>PHP cURL

  Laravel 6 Print Last Query(Debugging Queries)