php - Access Code not generated after CURL request

654

I have a URL, if I request from browser with this URL it will generate a code in the browser title bar itself, but whenever I am requesting through CURL, it is showing

{"message":"'UU8zSElJenNBTTRxakV1Y1FreUhCMkZlbnhiWm4xY003Vm55alBKVjpiM3pwcnpl cnQz' not a valid key=value pair (missing equal-sign) in Authorization header: 'Basic UU8zSElJenNBTTRxakV1Y1FreUhCMkZlbnhiWm4xY003Vm55alBKVjpiM3pwcnplcnQz'."}"

Actual URl is like is:

https://api.upstox.com/index/dialog/authorize?apiKey=QO3HIIzsAM4qjEucQkyHB2Fetytg1cM7VnyjPJV&redirect_uri=http://127.0.0.1:3000/&response_type=code

<?php

$url      = "https://api.upstox.com/index/dialog/authorize";
$api_key  = "QO3HIIzsAM4qjEucQkyHB2Fn1cM7VnyjPJV";
$uri      = "http://127.0.0.1:3000/";
$username   = "QO3HIIzsAM4qjEucQkynxbZn1cM7VnyjPJV";
$password   = "b3zprt3";


$data = array("apiKey" => $api_key, "redirect_uri" => 
"http://127.0.0.1:3000/","response_type" => "code");
$data_string = json_encode($data);

$curl     = curl_init();

curl_setopt_array($curl, array(

CURLOPT_URL             => $url,
CURLOPT_USERPWD         => "$username:$password",
CURLOPT_RETURNTRANSFER  => true,
CURLOPT_HEADER          =>  0,
CURLOPT_POSTFIELDS     => $data_string,
));

$result = curl_exec($curl);
//curl_close($curl);
var_dump($result);

?>

After running that code I am getting following message:

{"message":"'UU8zSElJenNBTTRxakV1Y1FreUhCMkZlbnhiWm4xY003Vm55alBKVjpiM3pwcnplcnQz' not a valid key=value pair (missing equal-sign) in Authorization header: 'Basic UU8zSElJenNBTTRxakV1Y1FreUhCMkZlbnhiWm4xY003Vm55alBKVjpiM3pwcnplcnQz'."}
800

Answer

Solution:

you can use:

curl_setopt($curl, CURLOPT_HTTPHEADER, [ "Authorization: Basic ".base64_encode($this->username.":".$this->password), ]);

or add this:

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

People are also looking for solutions to the problem: wordpress - How can i fetch numbered URLs that contain zero in PHP?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.