I am trying to get authorization code in php by REST api post request on "https://api.fyers.in/api/v1/auth" but I am get an error "{"ERROR": "No JSON object could be decoded"}"

I am trying to get authorization code in php by REST api post request on “https://api.fyers.in/api/v1/auth” but I am get an error “{“ERROR”: “No JSON object could be decoded”}”

I’ve had to add these extra curl options for it to work.

$curl = curl_init();

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

check them.

Encode using json function and use app_id/secret instead of http_build_query()

I don’t know where you got button submit and why you are using it.

$step1 = array('app_id'=>'XXXXX', 'secret_key'=>'YYYYY');
$post_str = json_encode( $step1 );

Hi Chirag, thanks for the help. I have tried this one and still the error is same. Below is the code i have used. Can you please look at this and help me.

‘xxxx’, ‘secret_key’=>‘yyyy’);
$post_str = json_encode( $step1 );

echo $post_str;

//open connection
$ch = curl_init();

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($curl, CURLOPT_HTTPHEADER, array(‘Content-Type:application/json’));

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post_str);

//So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

//execute post
$result = curl_exec($ch);
echo $result;

?>

It is simple PHP, you should read properly what the variable names are.

If you are using $ch and I am using $curl, then atleast that should be set accordingly instead of just copy-paste.

Thanks Chirag, its working…sometime silly mistakes also lead to trouble…once it has happened that by mistake i put double quote twice in code and it took about 2 hours to realize and detect the mistake.

@g8FKjVL444 you can atleast hit the like button.

I don’t know anything about cUrl I use Java HttpUrlConnection and Okhttp class but got a translation through post man check it works for me to get auth code

curl --location --request POST ‘https://api.fyers.in/api/v1/auth’ \

–header ‘sec-fetch-dest: empty’ \

–header ‘sec-fetch-mode: cors’ \

–header ‘sec-fetch-site: same-origin’ \

–header ‘Content-Type: application/json’ \

–data-raw '{

“app_id”:“XXXXX”,

“secret_key”:“XXXXXX”,

“create_cookie”: “False”

}’