How to upload a file to HubSpot, API


#1

I need to upload a file to HubSpot using PHP curl functions.

With OAuth 2.0 it doesn’t seem to work this way anymore. What am I doing wrong?

$url = “http://api.hubapi.com/filemanager/api/v2/files”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(“Content-Type: multipart/form-data”,“Authorization: Bearer $access_token”));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
“files”=> “@”.$filename,
‘file_name’ => ‘name.png’
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);

UPDATE:
I get such response

{“status”:“error”,“message”:“internal error”,“correlationId”:“bd6ea5a4-5ffd-4938-ada1-baf139a67b7a”,“requestId”:“a409f2e9a0204509beb430311b6fdd38”}

Can you provide me a working example?


How to upload file using HUBSPOT API PHP
#2

Same issue here. Tried also an equivalent to the listed python example as mentioned in the docs.


#3

+1 Having issues getting API uploads using PHP server side


#4
<?php

$params = "--custom-boundary-123456789\r\n".
		"Content-Disposition: form-data; name=\"folder_paths\"\r\n\r\n".
		"static\r\n".
		"--custom-boundary-123456789\r\n".
		"Content-Disposition: form-data; name=\"files[]\"; filename=\"file-a.html\"\r\n".
		"Content-Type: text/html\r\n\r\n".
		"<div>Content for file-a.html here...</div>\r\n".
		"--custom-boundary-123456789\r\n".
		"Content-Disposition: form-data; name=\"files[]\"; filename=\"file-b.html\"\r\n".
		"Content-Type: text/html\r\n\r\n".
		"<div>Content for file-b.html here...</div>\r\n".
		"--custom-boundary-123456789--";

$request_headers = array('Content-Type: multipart/form-data; boundary=--custom-boundary-123456789');

$ch = curl_init('http://api.hubapi.com/filemanager/api/v2/files?hapikey=my-api-key-here&overwrite=true');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);

$reply = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

?>

This is my example that I’m working with with no success


#5

Try to use CURLFile class for php 5.5.0+

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: multipart/form-data'
));

$hapikey = 'somekey';
$url = 'https://api.hubapi.com/filemanager/api/v2/files?hapikey=' . $hapikey ;
$aFiles = array();
foreach ($_FILES["attachments"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $filename = $_FILES['attachments']['name'][$key];
        $filedata = $_FILES['attachments']['tmp_name'][$key];
        if ($filedata != '') {
            $data = array("files" => new \CURLFile($filedata), "file_names" => $filename);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_exec($ch);
        }
    }
}

#6

For those who need a working code here's mine:

<? $url = "http://api.hubapi.com/filemanager/api/v2/files?hapikey=".$apikey;//change your api key here $realpath = '@'."background_2.jpg";//assuming we have background_2.jpg on same directory as the script $headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading $postfields = array("files"=>$realpath,'file_names'=>'test.jpg'); $ch = curl_init(); $options = array( CURLOPT_URL => $url, CURLOPT_HEADER => true, CURLOPT_POST => 1, CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => $postfields, CURLOPT_RETURNTRANSFER => true, ); // cURL options curl_setopt_array($ch, $options); $result = curl_exec($ch); echo $result; if(!curl_errno($ch)) { $info = curl_getinfo($ch); if ($info['http_code'] == 200) $msg = "File uploaded successfully"; echo $msg; } else { $errmsg = curl_error($ch); echo $errmsg; } curl_close($ch); ?>