The last example described in the PHP API Quickstart tutorial here: https://support.monday.com/hc/en-us/articles/360013465659-API-Quickstart-Tutorial-PHP does not work out of the box.
This example indicates that variables should be passed in as part of the request in a separate vars object passed alongside the query object; modified ever so slightly to work with text columns
<?php
$token = 'tokenstring';
$apiUrl = 'https://api.monday.com/v2';
$headers = ['Content-Type: application/json', 'Authorization: ' . $token];
$query = 'mutation ($myItemName: String!, $columnVals: JSON!) { create_item (board_id:XXXXXXXXX, item_name:$myItemName, column_values:$columnVals) { id } }';
$vars = ['myItemName' => 'Hello world!',
'columnVals' => json_encode([
'long_text5' => ['text' => '0400000000'],
'phone3' => ['text' => 'asd']
])];
$test_encoded = json_encode(['query' => $query, 'variables' => $vars]);
$data = @file_get_contents($apiUrl, false, stream_context_create([
'http' => [
'method' => 'POST',
'header' => $headers,
'content' => $test_encoded,
]
]));
$responseContent = json_decode($data, false);
echo json_encode($responseContent);
?>
It also seems to indicate the columnVals should be json_encoded at the time of assignment. Encoding columnVals here, however, AND again when constructing the content array as part of the $data request seems to lead to an error response from the API. The request sent to the API is valid JSON:
$test_encoded just after assignment (Valid JSON):
{“query”:“mutation ($myItemName: String!, $columnVals: JSON!) { create_item (board_id:XXXXXXXXX, item_name:$myItemName, column_values:$columnVals) { id } }”,“variables”:{“myItemName”:“Hello world!”,“columnVals”:“{"long_text5":{"text":"0400000000"},"phone3":{"text":"asd"}}”}}
Yet the response seems to indicate php is sending a PHP array instead of JSON as part of the request?
{"error_code":"ColumnValueException","status_code":200,"error_message":"invalid value, please check our API documentation for the correct data structure for this column. https://api.developer.monday.com/docs/change-column-values","error_data":{"column_value":"{\"text\"=\u003e\"asd\"}","column_type":"TextColumn"}}
I’m under no illusion there is something simple I’m missing, or a flag that needs to be set somewhere - would be good to understand how this works, and maybe updating the example with a more complex column type requiring a deeper associative array like email.
Any help is greatly appreciated!
Cheers,