Sending file with API

Hi there,

I have the code below:

	public function AsanaUpload($url){
		$url = ''.$url;
        $rsIntegration = $this->Integration($_COOKIE["idRefSite"], 1);
        $arIntegration = $rsIntegration->fetch(PDO::FETCH_OBJ);

        $headers = array('Authorization: Bearer '.$arIntegration->AsanaBearer.'');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->fields));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        return json_decode($output, true);

And I’m calling the function:

                             $filename = realpath("test.jpeg");
                            $erp->fields['file'] = "@".$filename . ";";
                            $attach = $erp->AsanaUpload('tasks/NUMBER OF THE TASK/attachments');

Error 1:
Array ( [errors] => Array ( [0] => Array ( [message] => file: File is not an object [help] => For more information on API status codes and how to handle them, read the docs on errors: Build an app with Asana ) ) )

And sometimes ERROR 2:
Array ( [errors] => Array ( [0] => Array ( [message] => file: Missing input [help] => For more information on API status codes and how to handle them, read the docs on errors: Build an app with Asana ) ) )

I just went here Build an app with Asana

My question is: what am I missing? I’d like to use only the file path without sending the file via FORM but is not working… Please let me know if you can help me with. Thanks!!

I tried to create this header:

$headers = array('Authorization: Bearer '.$arIntegration->AsanaBearer.'', 'Content-Type: multipart/form-data; boundary=13242', 'User-Agent: Java/1.7.0_76', 'Host: localhost', 'Accept: */*', 'Connection: keep-alive', 'Content-Length: 141');

But when I run the code it’s takes a while and nothing happens or timeout…

Hey there!

Attachments can be pretty tricky to get right, since they use a multipart form upload, and so are different than just a standard json payload like the rest of our API. I just double-checked that our php client library makes this a little bit less painful; here’s a quick working example:

require dirname(__FILE__) . '/vendor/autoload.php';  // This is using Composer for 
use Asana\Client;
getenv('ASANA_PERSONAL_ACCESS_TOKEN'); // Get my access token from the 
shell environment
$ASANA_TEST_TASK_ID = getenv('ASANA_TEST_TASK'); // the ID of a task I use for 
$client = Asana\Client::accessToken($ASANA_PERSONAL_ACCESS_TOKEN);

try {
  $file_data = file_get_contents("./pink-elephant.jpg");
  $file_name = "pink-elephant.jpg";
  $content_type = "image/jpeg";
  $attachment = $client->attachments->createOnTask($ASANA_TEST_TASK_ID, 
$file_data, $file_name, $content_type);
} catch (Asana\Errors\InvalidRequestError $e) {

If you’re just using standard PHP curl, I believe that as this StackOverflow question indicates the “modern” way of doing this is with the CURLFile class, but I don’t have an example of that handy. Hopefully this will put you on the right path!