PHP Example For File Upload

How in the world do I upload a file using the example PHP code format:

$update_id = "XXXXXX";

$query = "
		mutation {
			add_file_to_update (update_id: ".$update_id.", $file: !File) {
				id
			}
		}";

 $data = @file_get_contents($endpoint.'file', false, stream_context_create([
		    'http' => [
		        'method' => 'POST',
		        'header' => $endpoint_headers_file,
		        'content' => json_encode(['query' => $query]),
		    ]
		]));

Hey @BostonJames - let me check in with the team on this to confirm. More to come.

-Daniel

Thank you! Even the full Postman details would help. But for update not file column.

Hey @BostonJames,

Postman is a bit easier :slight_smile: we have a community post from Dipro going over doing this through Postman. Essentially you would need send your data as a multipart request (Content-Type: multipart/form-data) with the file in a variable.

This actually brings another thing to mind on the PHP side of things - I think it would be a good idea to use CURL in this case to send the file. Using the same multipart/form-data content type as above, I think you would be able to send these using CURL. There is a StackOverflow thread discussing this approach.

Hopefully this helps! Let me know if you have any other questions.

-Daniel

I’ve been trying all of the above but I get the following response:

“error_message”: “Unsupported query”,
“status_code”: 400

The query I’m using is:

mutation ($file: !File) { 
    add_file_to_update (update_id: XXXXXXXX, $file: !File) {
        id 
    } 
}

With variables[file] = local file selected.

Hey @BostonJames - could you try changing the beginning of the query?

mutation ($file: File!) { 
 add_file_to_update (file: $file, update_id: xxxxxx) { 
         id 
     } 
} 

-Daniel

That did it, thanks!

1 Like