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

LVL 1
BRDigital MarketingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

skullnobrainsCommented:
just add it to the array of CURLOPT_HTTPHEADERs line 25

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json',"Authorization: Basic " . base64_encode($ClientID . ':' . $ClientSecret)));

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
I would do it like this
$headers = [
  'Content-Type:application/json',
  'Authorization: Basic ' . base64_encode("{$ClientID}:{$ClientSecret}")
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Open in new window

There is much to be said for neat layout and code clarity.
BRDigital MarketingAuthor Commented:
thank you both
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Julian HansenCommented:
You are welcome.
gr8gonzoConsultantCommented:
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);
BRDigital MarketingAuthor Commented:
Thank you so much @gr8gonzo
skullnobrainsCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.