PHP API Tutorial Problems

I am attempting to follow the PHP tutorial located here:

All but the last example work, unfortunately that is the one I need.

This is what I have. it is taken directly from the tutorial only with my info substituted:



<?php

$token = 'THIS_IS_MY_API_KEY_THERE_ARE_MANY_LIKE_IT_BUT_THIS_ONE_IS_MINE';

$apiUrl = 'https://api.monday.com/v2';

$headers = ['Content-Type: application/json', 'Authorization: ' . $token];

$query = 'mutation ($myItemName: String!, $columnVals: JSON!) { create_item (board_id:MY_BOARD_ID_HERE, item_name:$myItemName, column_values:$columnVals) { id } }';

$vars = ['myItemName' => 'Hello world!',

'columnVals' => json_encode([

'text' => ['text' => 'Done'],

'text7' => ['text' => '19930827']

])];

$data = @file_get_contents($apiUrl, false, stream_context_create([

'http' => [

'method' => 'POST',

'header' => $headers,

'content' => json_encode(['query' => $query, 'variables' => $vars]),

]

]));

$responseContent = json_decode($data, true);

echo json_encode($responseContent);

?>

This is the Result:

{"error_code":"ColumnValueException","status_code":200,"error_message":"invalid value, please check our API documentation for the correct data structure for this column. https:\/\/developer.monday.com\/api-reference\/docs\/change-column-values","error_data":{"column_value":"{\"text\"=>\"Done\"}","column_type":"TextColumn"}}

It looks like others are having the same issue:

Just trying to find a way to add an item with column data via PHP,

Any help much apprecieated.

Hello @hderic!

I see you created a ticket with us about this.

I have replied over there :slightly_smiling_face:

Cheers,
Matias

Thank you Matias! Got it to work using the PHP curl example you emailed me. I thought Id post the examples you sent here incase they can help the next person.

FYI I only tested the first one (PHP cUrl)

PHP - cURL:

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.monday.com/v2',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{"query":"mutation ($myColumnValues: JSON!) { create_item ( board_id: 123456789, group_id: \\"topics\\", item_name: \\"My Item\\", column_values: $myColumnValues ) { id } }","variables":{"myColumnValues":"{\\"text\\":\\"Some text\\"}"}}',
CURLOPT_HTTPHEADER => array(
'Authorization: MYAPIKEYHERE',
'Content-Type: application/json',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

PHP Guzzle:

<?php
$client = new Client();
$headers = [
'Authorization' => 'MYAPIKEYHERE',
'Content-Type' => 'application/json',
];
$body = '{"query":"mutation ($myColumnValues: JSON!) { create_item ( board_id: 123456789, group_id: \\"topics\\", item_name: \\"My Item\\", column_values: $myColumnValues ) { id } }","variables":{"myColumnValues":"{\\"text\\":\\"Some text\\"}"}}';
$request = new Request('POST', 'https://api.monday.com/v2', $headers, $body);
$res = $client->sendAsync($request)->wait();
echo $res->getBody();

PHP HTTP Request 2:

<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://api.monday.com/v2');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
'follow_redirects' => TRUE
));
$request->setHeader(array(
'Authorization' => 'MYAPIKEYHERE',
'Content-Type' => 'application/json',
));
$request->setBody('{"query":"mutation ($myColumnValues: JSON!) { create_item ( board_id: 123456789, group_id: \\"topics\\", item_name: \\"My Item\\", column_values: $myColumnValues ) { id } }","variables":{"myColumnValues":"{\\"text\\":\\"Some text\\"}"}}');
try {
$response = $request->send();
if ($response->getStatus() == 200) {
echo $response->getBody();
}
else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
}
}
catch(HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}

PHP pecl_HTTP:

<?php
$client = new http\Client;
$request = new http\Client\Request;
$request->setRequestUrl('https://api.monday.com/v2');
$request->setRequestMethod('POST');
$body = new http\Message\Body;
$body->append('{"query":"mutation ($myColumnValues: JSON!) { create_item ( board_id: 1234567890, group_id: \\"topics\\", item_name: \\"My Item\\", column_values: $myColumnValues ) { id } }","variables":{"myColumnValues":"{\\"text\\":\\"Some text\\"}"}}');
$request->setBody($body);
$request->setOptions(array());
$request->setHeaders(array(
'Authorization' => 'MYAPIKEYHERE',
'Content-Type' => 'application/json',
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();

Thanks again!

1 Like

Hello there @hderic,

Thank you for sharing this!!

Cheers,
Matias