Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

cURL header in PHP

How can I add this to my existing header?

$header = "Authorization: Basic " . base64_encode($ClientID . ':' . $ClientSecret);

My code is like below. And I need to add it to my header. Thank you

<?php

$ClientID = "myclientid";

$ClientSecret = "myclientsecret";

$url = 'https://mysitelink...';


//create a new cURL resource
$ch = curl_init($url);

//setup request to send json via POST
$data = array(
    'username' => 'myusername',
    'password' => 'mypassword',
);

$nkolay = json_encode(array($data));

//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $nkolay);

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


//return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute the POST request
$result = curl_exec($ch);

//burada linkteki sayfadan datayı alıyorum. 
var_dump($result);

//close cURL resource
curl_close($ch);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of skullnobrains
skullnobrains

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

thank you both
You are welcome.
By the way, this is what the CURLOPT_USERPWD option does for you (base64-encodes the content and adds it to the headers). Usage:

curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $password);
Avatar of BR

ASKER

Thank you so much @gr8gonzo
Avatar of skullnobrains
skullnobrains

also note that you can do the same thing without curl using a regular stream_get_contents with proper context options
http://php.net/manual/en/context.http.php