Saturday, June 4, 2022

[SOLVED] How to pass a http header using a variable in curl using PHP

Issue

My code to add header is:

$apikey="xyz";
     curl_setopt($handle, CURLOPT_HTTPHEADER, array(
                                            'Content-Type: application/json',
                                            'Accept: application/json',
                                            'X-Auth-Token: $apikey'                                 
                                            ));

But I am getting an error showing that **This request requires HTTP Authentication.**Now I want to know whether I am right? If Yes why I am getting error.If I am wrong how can do this..

Thankyou,


Solution

Use double quotes if you want your variable to be interpolated by PHP, i.e. "X-Auth-Token: $apikey".

For more info see PHP: Strings.



Answered By - Giedrius D
Answer Checked By - Willingham (WPSolving Volunteer)