Unexpected Token in Json When Using cURL from Php

Unexpected Token in Json When Using cURL from Php

Im using Graphql on remote server and php+curl on main server, when im tryin go update or creat new im got an error "Unexpected token ** in JSON at position"

I'm really trying to shielding but is is doesn't help. If im made request from graphql frontend it is works fine.

Fine JSON request:

mutation {
  updateMarket(Id: "002384fa-7d45-e911-9c76-00155d056158", Lat: 122, Lng: 1) {
    Id
    Lat
  }
}

Php code:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, '192.168.1.1:4040/graphql');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "mutation {\n  updateMarket(Id: \"002384fa-7d45-e911-9c76-00155d056158\", Lat: 122, Lng: 1) {\n    Id\n    Lat\n  }\n}");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
1

2 Answers

After some times of research Im find a lib "gmostafa/php-graphql-client" (composer require) It is nice instrument for make querys and mutation for Graphql API from php.

It's not correct JSON syntax. What do you want from this "mutation" row?

2

Your Answer

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

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.