Sergio Suarez
Sergio Suarez

Reputation: 117

how to POST CURL with data and headers in PHP

i would like to make a CURL to get an auth tokken.

The platform dev forum give me that:

curl -X POST \
  --header 'Content-Type: application/json; charset=utf-8' \
  --header 'Accept: application/json' \
  -d '{"email":"MY_EMAIL","password":"MY_PASSWORD"}' \
  'https://api.voluum.com/auth/session'

How do i make that work in PHP?

Upvotes: 1

Views: 171

Answers (3)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

You can do it like below:-

<?php                                                             
$data_string = '{"email":"MY_EMAIL","password":"MY_PASSWORD"}';                                                                                   

$ch = curl_init('https://api.voluum.com/auth/session');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json; charset=utf-8',
    'Accept: application/json'
));                                                                                                                 

$result = curl_exec($ch);
if (curl_errno($ch)) {
   echo 'Error:' . curl_error($ch);
  exit;
}
curl_close ($ch);
var_dump($result);

I run it and found below response (because i don't have mail-id and password):- https://prnt.sc/gdz82r

But the pleasant part is code executed successfully and when you will provide right credentials then it will give you correct output.

Upvotes: 0

Naincy
Naincy

Reputation: 2943

$vars = '{"email":"MY_EMAIL","password":"MY_PASSWORD"}'; 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.voluum.com/auth/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);  //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = ['Content-Type: application/json; charset=utf-8', 
'Accept: application/json']; 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$server_output = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
    exit;
}
curl_close ($ch);

print_r($server_output);

Upvotes: 1

Bananaapple
Bananaapple

Reputation: 3114

Try this: https://incarnate.github.io/curl-to-php/

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.voluum.com/auth/session");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"email\":\"MY_EMAIL\",\"password\":\"MY_PASSWORD\"}");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Content-Type: application/json; charset=utf-8";
$headers[] = "Accept: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

Upvotes: 1

Related Questions