I’m working on a script to allow people to upload files to a service on my company’s servers. This service has a REST API to handle requests, so I’m working with cURL to upload the files. The documentation on the service’s website gives this command-line cURL request as an example:
curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "[email protected]" http://myhost/rest/api/2/issue/TEST-123/attachments
I’m trying to duplicate this in PHP, and my function currently looks like this:
[php]
/**
- Uploads a file to JIRA as an attachment to the given issue.
- @input $issue - The issue key of the target task
- @input $file - The file (as drawn from the $_FILES superglobal) to upload.
-
@return - The result of executing the cURL query.
*/
function curl_upload_attachment($issue,$file){
$access = curl_init(“http://[redacted]/rest/api/2/issue/”.$issue."/attachments");
curl_setopt($access,CURLOPT_USERPWD,$GLOBALS[‘user_info’]);
curl_setopt($access,CURLOPT_RETURNTRANSFER,1);
curl_setopt($access,CURLOPT_POST,true);
curl_setopt($access,CURLOPT_POSTFIELDS,array(‘file’ => $file[‘tmp_name’]));
curl_setopt($access,CURLOPT_HTTPHEADER,array(‘X-Atlassian-Token: nocheck’)); //Necessary for XSRF
$result = curl_exec($access);
curl_close($access);
return $result;
}[/php]
The function currently returns a 500 error from the servers, which means my data is formatted incorrectly in some way. It’s meant to be encoded as multipart/form-data, which it is, but beyond that I have no idea where the error is coming from. Could someone help me duplicate that command-line query in PHP a bit better?