Home » PHP cUrl GET & POST Methods Example

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.

Read Also:  Removing # hash from URL in AngularJS

“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.”

Read Also:  Woocommerce Get product category name in array

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

Read Also:  Image Uploads with Angular 9 Tutorial

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Javascript Autocomplete Address Using PHP With Demo
  2. Read Also:  Bind Array List to Checkboxes using Angular Example
  3. Read Also:  clear cache laravel
  4. Read Also:  How to convert base64 to image and save in folder php?
  5. Read Also:  How to Get the Current URL with VueJS?
  6. Read Also:  Vuejs tree view Components And Modules - Vue Tree View Component
  7. Read Also:  Create WordPress Custom Admin Page
  8. Read Also:  How to Get the Current URL with VueJS?
  9. Read Also:  Woocommerce Get product category name in array
  10. Read Also:  Removing # hash from URL in AngularJS

Leave a Comment

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