Syntaxerror: JSON.parse: Unexpected Character at line 1 column 1 of the JSON data

Asked

Viewed 2,339 times

0

<?php

session_start();
header('Content-type: application/json');

$email = "XXXX"; 
$password = "XXX"; 
$serviceId = 4728;

$url = 'https://login.globo.com/api/authentication';

$jsonAuth = array(
'captcha' => '',
'payload' => array(
 'email' => $email,
 'password' => $password,
 'serviceId' => $serviceId
    )
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($jsonAuth));
$result = curl_exec($ch);
$obj=json_decode($result);
echo "id: " . $obj->{'glbId'};

?>

My code is this, when running in the browser appears the error: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Anyone can help?

2 answers

2

You just take the header('Content-type: application/json'); the browser is assuming that its content is JSON and tries to format.

0

you don’t need keys to access the object. Thus:

$obj->glbId;

  • Thank you. I got it

  • Tassio can you please mark the answer as correct, just so you signal that the question has been answered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.