Sending a file via POST using PHP cURL

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?

Print the $result variable and paste it in a code block.

The result variable provides no new information; it’s just a big ol’ block of html that says the server encountered a 500 error, but alright.

[code]

Apache Tomcat/6.0.32 - Error report

HTTP Status 500 -


type Status report

message

description The server encountered an internal error () that prevented it from fulfilling this request.


Apache Tomcat/6.0.32

[/code]

Will you post the entire script not just this little function? Remember to concat any passwords and other sensitive info.

What I am trying to determine is the structure of the variable you are passing though cURL to see if it matches your template.

Other information is minimal, and I’ve fairly thoroughly vetted it for errors. The form that submits and processes this data is here:
[php]<?php require_once ‘…/includes/php_header.php’; //Not related to this, handles requires, scripts, and css inclusion
$issue = curl_get_issue($_GET[‘key’]); //This function works flawlessly and is unrelated.
if ($_FILES != array()){ //If a file is successfully uploaded
$result = curl_upload_attachment($_GET[‘key’],$_FILES[‘file’]);
}
?>

Uploading attachment to <?php echo $issue['key'],': ',$issue['fields']['summary']; ?>

File must be 10 MB or less

[/php]

I can look at/through snip its all day and not see the error or you could put the entire script up and we could try and figure out the problem.

or you can just forget all the cURL BS and issue a shell command like the example

instructions available here http://php.net/manual/en/function.shell-exec.php

You cannot solve a puzzle if you don’t have all the pieces.

I’ve sent you the entire body of code that even remotely involves this. Believe me, you have all the pieces. I’m not intentionally withholding any information. You’ve received the form that takes a file from the user, uploads it to the server, and stores it in the $_FILES superglobal. The relevant file is then sent to the curl_upload_attachment method, which I’ve posted in full. It relies on no external methods. I’ve told you everything about the documentation that needs to be known.

Please don’t act like I’m trying to hide the solution to the problem from you. I’m trying to get help.

As for cURL, I’m using it because the rest of this application (into which I’ve sunk about eighty hours of work) uses cURL as well, with no issues. None of it relates even indirectly to this function, before you ask.

Sponsor our Newsletter | Privacy Policy | Terms of Service