ERROR: Apostrophe's in Board Updates

I’m using the following and it works unless there’s an apostrophe in the update. I’ve tried addslashes() but that didn’t do it.

// send to Monday.com - update
	$board_id = 'XXXXXXXX';

	$content = "This isn't working";

	$query = '
	mutation {
		create_update (item_id: '.$board_id.', body: "'.$content.'") {
			id
		}
	}';
					
	$data = @file_get_contents($endpoint, false, stream_context_create([
	    'http' => [
	        'method' => 'POST',
	        'header' => $endpoint_headers,
	        'content' => json_encode(['query' => $query], JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE),
	    ]
	]));

Hey @BostonJames,

first of all - welcome to the community!

Could you expand your question a bit, not exactly sure what the issue is :slight_smile: .

Greetings.

If there are single quotes or double quotes in my post title or content it doesn’t work. For example:

This is my post title and it works

Vs

This is my post title and it doesn’t work

I tried addslashes() and the various options for json_encode() for quotes but it isn’t working.

Did you try to escape?

Looks like this:

$content = "This isn\'t working";

2 Likes

Hey @BostonJames - could you try the example @TMNXT-Dev posted above? I think that should do the trick here.

-Daniel

This is very confusing. This works:

	$content = 'This wasn\'t posting to Monday.com before.';

	$query = '
	mutation {
		create_update (item_id: '.$ticket['monday_board_id'].', body: "'.$content.'") {
			id
		}
	}';

But this does not:

	$content = addslashes($input['content']);

	$query = '
	mutation {
		create_update (item_id: '.$ticket['monday_board_id'].', body: "'.$content.'") {
			id
		}
	}';

Even though when I echo this:

$content = addslashes($input['content']);
echo $content;

I get this:

This wasn\'t posting to Monday.com before.

Hey @BostonJames - stepping back a bit I think we can skip the addslashes method all together.

Using our PHP quickstart as a baseline, I made a few modifications which allowed me to upload updates through the GraphQL API without any hiccups.

In this example I’m using the $input variable since I was manually entering different variables on the terminal, but you can replace it for a pre-determined string. The biggest change was declaring the variables on the query portion of the query so that json_encode would be able to process it.

echo "What do you want to input? ";
$input = rtrim(fgets(STDIN));

$query = 'mutation ($myUpdate: String!) { create_update (item_id: xxxx, body:$myUpdate) {id}}'; 
$vars = ['myUpdate' => $input];

And then you would just need to modify the last line of the $data portion to json_encode / send your variables as well as the query:

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

Let me know if this helps!

-Daniel

That did the trick, thank you!

1 Like