7 Replies - 265 Views - Last Post: 08 December 2017 - 03:41 AM Rate Topic: -----

#1 mutago234  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 248
  • Joined: 08-September 13

PHP Json Curl shows Error reading from JSON Reader

Posted 06 December 2017 - 02:52 AM

Within the body of API POST we define the parameters of the group. For example:
{
  "description": "An Awesome New Group",
  "displayName": "Awesome Group",
  "groupTypes": ["Unified"],
  "mailEnabled": true,
  "mailNickname": "awesomeGroup",
  "securityEnabled": false
}



Now I want to send the variables/parameters over to the API but it displays error

An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartArray' node was expected.



Below is my Code
<?php 
session_start();


echo $acc= $_SESSION['access_token'];

$data_string = array("description" => "An Awesome New Group", "displayName" => "Awesome Group", "groupTypes" => "Unified", "mailEnabled" => "true", "mailNickname" => "awesomeGroup", "securityEnabled" => "fasle"); 
$data = json_encode($data_string); 
//$data = $data_string; 
$curl = curl_init();

curl_setopt_array($curl, array( 
CURLOPT_URL => "https://example.com/groups",

CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true, 
CURLOPT_ENCODING => "", 
CURLOPT_MAXREDIRS => 10, 
CURLOPT_TIMEOUT => 30, 
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
CURLOPT_CUSTOMREQUEST => "POST", 
CURLOPT_POSTFIELDS => "$data", 
CURLOPT_HTTPHEADER => array( 
"accept: application/json", 
"authorization: Bearer $acc", 
"content-type: application/json; charset=utf-8" 
), 
));

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

curl_close($curl);



print_r($response);




if ($err) { 
echo "cURL Error #:" . $err; 
} else { 
//echo $response; 
}
?>



I think the problem if how json data to be sent over the API were arranged. Please can someone help me fix the issue

This post has been edited by mutago234: 06 December 2017 - 02:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: PHP Json Curl shows Error reading from JSON Reader

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4139
  • View blog
  • Posts: 13,089
  • Joined: 08-June 10

Re: PHP Json Curl shows Error reading from JSON Reader

Posted 06 December 2017 - 03:10 AM

groupTypes needs to be an array, not a single value.
Was This Post Helpful? 0
  • +
  • -

#3 mutago234  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 248
  • Joined: 08-September 13

Re: PHP Json Curl shows Error reading from JSON Reader

Posted 06 December 2017 - 10:54 AM

Please Sir, how do I send it as an array. I have tried.
"groupTypes" => ["Unified"]

This post has been edited by mutago234: 06 December 2017 - 10:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1863
  • View blog
  • Posts: 5,823
  • Joined: 15-January 14

Re: PHP Json Curl shows Error reading from JSON Reader

Posted 06 December 2017 - 12:14 PM

That should work, that's an array with 1 item.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3721
  • View blog
  • Posts: 13,512
  • Joined: 08-August 08

Re: PHP Json Curl shows Error reading from JSON Reader

Posted 06 December 2017 - 08:25 PM

$data_string needs to be an object, not an array. And it shouldn't be called a string!
$obj = new stdClass();

$obj->description = "A Description!";
$obj->displayName = "Awesome name";
$obj->groupTypes = ["unified"];

$json = json_encode($obj);
echo $json;


Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3721
  • View blog
  • Posts: 13,512
  • Joined: 08-August 08

Re: PHP Json Curl shows Error reading from JSON Reader

Posted 06 December 2017 - 08:40 PM

A shorter way to write it:
$obj = (object)["description"=>"A Description!", "displayName" => "Awesome name", "groupTypes" => ["unified"]];

$json = json_encode($obj);
echo $json;


Was This Post Helpful? 0
  • +
  • -

#7 mutago234  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 248
  • Joined: 08-September 13

Re: PHP Json Curl shows Error reading from JSON Reader

Posted 08 December 2017 - 03:40 AM

Thanks everyone for help so far. At CTphpnwb I have implemented your solution as per code below but it now shows error
 [b]{ "error": { "code": "Request_BadRequest", "message": "Cannot convert a primitive value to the expected type 'Edm.Boolean[/b]


Below is my implementation base on CTphpnwb suggeestion.

$obj = (object)["description"=>"A Description!","mailEnabled" => true,"mailNickname" => "awesomeGroup",  "securityEnabled" => fasle,"displayName" => "Awesome name", "groupTypes" => ["unified"]];



This post has been edited by mutago234: 08 December 2017 - 03:51 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4139
  • View blog
  • Posts: 13,089
  • Joined: 08-June 10

Re: PHP Json Curl shows Error reading from JSON Reader

Posted 08 December 2017 - 03:41 AM

fasle is not a Boolean.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1