0

PHP Curl Cannot send HTTP HEADER Authorization Secret.

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_POST => 1,
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_HTTPHEADER, array( 'Authorization: Secret <secret>' )
));

$result = curl_exec($curl);
curl_close($curl);
echo $result;

In <secret>, I am sending secret. But Showing error is authorization header null

Curl getinfo is

Array
(
    [url] => http://x.x.x.x:8090/token/create
    [content_type] => application/json
    [http_code] => 400
    [header_size] => 158
    [request_size] => 142
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 1.672836
    [namelookup_time] => 4.6E-5
    [connect_time] => 1.331548
    [pretransfer_time] => 1.331722
    [size_upload] => 0
    [size_download] => 37
    [speed_download] => 22
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 1.672799
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => x.x.x.x
    [certinfo] => Array
        (
        )

    [primary_port] => 8090
    [local_ip] => x.x.x.x
    [local_port] => xxxx
)

When I am using Postman Or Rest Client. I am sending Authorization Secret. Then Response is coming successfully.

Do you any Suggestion? Why PHP Curl is showing error but Postman or Rest Client is working successfully?

1 Answer 1

2

Use this:

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_HTTPHEADER => array(
        "Authorization: Secret <secret>"
      )
));

$result = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

echo $result;
Sign up to request clarification or add additional context in comments.

4 Comments

It's not because of quotation. It's because of =>. you should use this: CURLOPT_HTTPHEADER => array( "Authorization: Secret <secret>" )
Also you can view the php curl code from postman. Go to the postman, under Send button there are two link: Cookies and Code. Click on Code link. Then select php Curl in drop down list. You will see the php curl code there.
Result is working successfully when using postman. But Curl is not working
You can get php curl code from postman. In postman there is an orange colour code link under Send button. Click on that and select php curl from drop down list. There you will get the whole php curl code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.