I
I
Ivan Budakov2020-01-01 12:40:16
PHP
Ivan Budakov, 2020-01-01 12:40:16

how to send json to php?

Happy New Year everyone)

There is JSON and PHP code.

The JSON sends the required content on Monday and creates the item, while PHP doesn't work.
Where did I make a mistake in trying to rewrite the working JSON? Thanks =)

JSON:

const body = {
              query: `
                mutation {
                  create_item (board_id: 748486084, group_id: "new_group88144", item_name: "new item") {
                    id
                  }
                }
              `,
              variables: {
                boardId: 748486084,
                groupId: "new_group88144",
                itemName: "new item",
              }
            }

            fetch('http://api.monday.com/v2/', {
                method: 'POST',
                headers: {
                  'Content-Type': 'application/json',
                  'Authorization': 'TOKEN',
                },
                body: JSON.stringify(body),
              })
              .then(response => response.json());


PHP:
$url = 'http://api.monday.com/v2/';
            $dataquery = '"Authorizationl:TOKEN" query: `mutation { create_item (board_id: 748486084, group_id: "new_group88144", item_name: "new item") {id}}`';

            $options = array(
                'http' => array(
                    'header'  => "Content-type: application/json",
                    'method'  => 'POST',
                    'body' => http_build_query($dataquery),
                )
            );
            $context  = stream_context_create($options);
            $result = file_get_contents($url, false, $context);
            if ($result === FALSE) { /* Handle error */ }

            echo($result);
            var_dump($result);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Budakov, 2020-01-01
@Meowwiza

In general, I re-read the API and the solution was as follows

$token = 'TOKEN';
$apiUrl = 'https://api.monday.com/v2';
$headers = ['Content-Type: application/json', 'Authorization: ' . $token];

$query = 'mutation{ create_item (board_id:id, group_id: "group", item_name:"name") { id } }';
$data = @file_get_contents($apiUrl, false, stream_context_create([
 'http' => [
 'method' => 'POST',
 'header' => $headers,
 'content' => json_encode(['query' => $query]),
 ]
]));
$responseContent = json_decode($data, true);

echo json_encode($responseContent);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question