Variables in Column_Values with PHP

Hey All,
New here to the community and have been playing around with your " API Quickstart Tutorial - PHP". I have been able to get pretty far playing around with things I need but I have come up with a stumbling block and all of my searching in the community I can’t seem to find anything so here I am!

I went ahead and did a copy and paste of your " Creating a new item using GraphQL variables" section and it worked great in my board but that was for the item name. How can I put a variable into a column_value value?

Here’s the code (with token and everything after $responseContent = removed):
$apiUrl = ‘https://api.monday.com/v2’;

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

$marketing = “Package A ($150)”;

$query = ‘mutation ($myItemName: String!) { create_item (board_id:My Board_ID, item_name:$myItemName, column_values : “{“title”:$myItemName}”) { id } }’;

$vars = [‘myItemName’ => $marketing];

$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);

Any help or pointing me in the right direction is greatly appreciated!

2 Likes

Hi @mabramsphoto and welcome to the community.

The snippet below adds an item to a board from PHP. There are a few constants that need to be changed to work in another account. Hope this will give you a glue.

//Create a new item in the board
$header = array(
‘User-Agent’ => MONDAY_TEAM . ’ GraphQL Client’,
‘Authorization’ => get_field(‘mondaykey’, ‘user_’ . get_current_user_id()),
‘Content-Type’ => ‘multipart/form-data’
);

//str_replace is used in an effort to get as close as possible to the original file name (sanitized by WPForms)
$body = array(
‘query’ => ‘mutation {create_item (board_id: ’ . $board_id . ‘, group_id: “topics”, item_name: "’ . str_replace("-"," “,pathinfo($upload[‘name’], PATHINFO_FILENAME)) . '”) {id}}’
);

$response = wp_remote_post( MONDAY_URL, [‘headers’ => $header, ‘body’ => $body] );
$arr1 = json_decode($response[‘body’], true);
$item_id = $arr1[‘data’][‘create_item’][‘id’];

Hi Basedbruin,
Thank you for your reply, I really appreciate it, but this has nothing to do with a file upload I am trying to get form data and put it into monday as a new item. when adding values into the column_values hard coded it works great, but I want to know how I can use a variable that was received from a $_POST into column_values.

Any help in that matter is definitely help full!

Hey @mabramsphoto :wave:

That’s a great question! There is a section in our PHP quickstart that sheds more light on how to create an item with column values populated - have you been able to test that out as well?

-Alex

hi @mabramsphoto
Sorry, didn’t understand you were looking for file upload. Actually the snippet I showed does continue (see below) and does do a file upload. The file should be available on your server (in my case a WordPress server). Hope you find your answer here.

/************************************************************************************************************************************************************

  • Name: monday_upload_files
  • Arguments: $board_id, $upload - board_id: DOCS board id where item needs to be created / $upload: array with file upload information
  • Returns: None
  • Function: Creates an item in the given board (first group) and attaches the file to the file column
    *************************************************************************************************************************************************************/

function monday_upload_files( $board_id, $upload) {

//Create a new item in the board
$header = array(
‘User-Agent’ => MONDAY_TEAM . ’ GraphQL Client’,
‘Authorization’ => get_field(‘mondaykey’, ‘user_’ . get_current_user_id()),
‘Content-Type’ => ‘multipart/form-data’
);

//str_replace is used in an effort to get as close as possible to the original file name (sanitized by WPForms)
$body = array(
‘query’ => ‘mutation {create_item (board_id: ’ . $board_id . ‘, group_id: “topics”, item_name: "’ . str_replace("-"," “,pathinfo($upload[‘name’], PATHINFO_FILENAME)) . '”) {id}}’
);

$response = wp_remote_post( MONDAY_URL, [‘headers’ => $header, ‘body’ => $body] );
$arr1 = json_decode($response[‘body’], true);
$item_id = $arr1[‘data’][‘create_item’][‘id’];

//Generate a boundary, needed for the Header (Content-Type) and the Body (to split in multiple parts)
$boundary = wp_generate_password(24);
$eol = “\r\n”;

$header = array(
‘User-Agent’ => MONDAY_TEAM . ’ GraphQL Client’,
‘Authorization’ => get_field(‘mondaykey’, ‘user_’ . get_current_user_id()),
‘Content-Type’ => ‘multipart/form-data; boundary=’ . $boundary
);

//We need to build the body from scratch because it is multipart with boundaries
//Take care of the extra empty lines (twice $eol) before the actual query and file, otherwise an error 500 is returned

$body = ‘–’ . $boundary . $eol;
$body .= ‘Content-Disposition: form-data; name=“query”’ . $eol . $eol;
$body .= 'mutation ($file: File!) {add_file_to_column (item_id: ’ . $item_id . ‘, column_id: file, file: $file) {id}}’ . $eol;
$body .= ‘–’ . $boundary . $eol;
$body .= ‘Content-Disposition: form-data; name=“variables[file]”; filename="’ . $upload[‘name’] . ‘"’ . $eol;
$body .= 'Content-Type: ’ . $upload[‘type’] . $eol . $eol;
$body .= file_get_contents($upload[‘value’]);
$body .= $eol . ‘–’ . $boundary . ‘–’;

$response = wp_remote_post( MONDAY_URL . ‘file/’, [‘headers’ => $header, ‘body’ => $body] );

unlink(ABSPATH . substr($upload[‘value’], 25));
}

Hi Alex,
I feel like a dumb a** for not seeing that right below!

Sooooooo, it’s almost there.

I tried to plug and play a few different things based on my board.

I did the date id, made it a date type and added a date just like your sample and and it worked great.

BUT when I tried one of my text type columns I got an error…

  • error_code: “ColumnValueException”,

  • status_code: 200,

  • error_message: “invalid value, please check our API documentation for the correct data structure for this column. API v2 Documentation”,

  • error_data: {

    • column_value: “{“text”=>“Michael Abrams”}”,

    • column_type: “TextColumn”}

Here is the code:
$query = ‘mutation ($myItemName: String!, $columnVals: JSON!) { create_item (board_id:XXXXXXXXXX, item_name:$myItemName, column_values:$columnVals) { id } }’;

$vars = [‘myItemName’ => $marketing,

‘columnVals’ => json_encode([

'text9' => ['text' => 'Michael Abrams']

])];

$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);

Also, I have another column which is id: “status87” type: “color”
i formatted it as ‘status87’ => [‘color’ => ‘Michael Abrams’] and it ran just fine but nothing was the column when I went to refresh.

I feel like we are definitely in the right direction, just need to figure out these little snafus.

I really appreciate your help!

Hi again basedbruin…

My apologies is I am not communicating it correctly. i am NOT looking to do a file upload, just looking to bring over some form data that was entered in by a client.

BUT you have given me a great solution possibly for another part that I am building, so I definitely appreciate that!!!

So going further in checking all of the column types in my board the only ones that actually populate from a creat item scenario is “Date” and “Phone”

Color and Dropdown types run fine but nothing is populated when refreshing and finding the record. It’s as if it thinks the column isn’t there and runs it no matter what because I made up a column id that doesn’t exist in the table and it ran no problem gave me a new item id and created the item on the board without errors.

Text and Numeric types produce the error that I showed above even though it’s exactly entered like the quick start.

very confusing

Ok figured out the “color” type!!! It’s actually a “label” type!!! when i changed it to

‘status87’ => [‘label’ => ‘XXXXXX’] it worked!! One step closer!

Text, Email and Numeric Types still don’t work

1 Like

Hi @mabramsphoto,

Text and Numeric should be set directly like this:

'text9' => 'Michael Abrams'

I had a hard time with that too :wink:

Laurent

2 Likes

Hi @mabramsphoto!

Adding on here, the email column should be formatting as described in our documentation here.

Essentially, this should be the formatting:

"{\"email\":\"itsmyemail@mailserver.com\",\"text\":\"my email\"}"

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.