PHP Post Request cURL Example With Authentication

  • 15-09-2022
  • 1588
  • PHP
  • Haresh Chauhan

cURL in PHP is a PHP extension that allows you to execute or use URLs to send post HTTP requests or get data from the server. cURL simple works between the Client side to server side and server side to client-side data submit and receive.

To use cURL you need just need to pass the request method and header for the type of getting and receiving form data on the server side. We just need to define each request method like getting & POST.

If the request is authenticated you can also pass tokens or API keys in the header to access server-side data using cURL in the PHP application.

<?php

$url = "https://reqbin.com/echo/post/json";

$headers = array(
     "Content-Type: application/json",
     //"Authentication : " // HERE YOU CAN PASS API KEY OR TOKEN
);

$data = '{"login":"my_login","password":"my_password"}';

$curl = curl_init($url);

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// HEADER SEND
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

// POST DATA SEND 
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

// FOR DEBUG ONLY!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);

curl_close($curl);

// FURTHER PROCESSING ...
if ($resp == "OK") { ... } else { ... }

?>

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :