Hubspot File Post Api Not Working

SOLVE
psama
Participant

$post_url = "https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=apikey";
$upload_file = new CURLFile('https://dl.airtable.com/.attachments/4b3ae7d535258081f030e2552b667693/6a41a2b5/download3.png','multi...');

$file_options = array(
"access" => "PUBLIC_INDEXABLE",
"ttl" => "P3M",
"overwrite" => true,
"duplicateValidationStrategy" => "NONE",
"duplicateValidationScope" => "ENTIRE_PORTAL"

);

$post_data = array(
"file" => $upload_file,
"options" => json_encode($file_options),
"folderPath" => "/docs"
);


echo '<pre>';
print_r($post_data);
echo '</pre>';

$ch = curl_init();
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_URL, $post_url);
@curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);

 


// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); //Log the response from HubSpot as needed.
print_r($response);

$image_url = json_decode($response ,true);
$url = $image_url['objects'][0]['url'];
print_r($url);
curl_close($ch);

0 Upvotes
1 Accepted solution

Accepted Solutions
psama
Solution
Participant

Hi @Teun 
Upload Api Works fine Now
this is working code. but I want to upload multiple images at the same time . can you give Idea about that?

<?php
$apikey = 'api key';
$url = "https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=" . $apikey;

$url2 = "https://growthnatives.com/wp-content/uploads/2020/04/logo.png";
$url3 = "https://dl.airtable.com/.attachments/c6bd26c43079ae47a60cdbb2e5e2105f/65f13185/610178d018133.png";
$pathinfo = pathinfo($url2);

$img= $pathinfo['filename'].'.'.$pathinfo['extension'];
// Function to write image into file
file_put_contents($img,file_get_contents($url2));

echo "File downloaded!";
$realpath = getcwd()."\\". $img ;

// cURL headers
$headers = array("Content-Type:multipart/form-data");
//$upload_file = new CURLFile($realpath,'application/octet-stream',$img);

$upload_file = new CURLFile($realpath,'application/octet-stream',$img);

$file_options = array(
"access" => "PUBLIC_INDEXABLE",
"ttl" => "P3M",
"overwrite" => false,
"duplicateValidationStrategy" => "NONE",
"duplicateValidationScope" => "ENTIRE_PORTAL"
);

$post_data = array(
"file" => $upload_file,
"options" => json_encode($file_options),
"folderPath" => "/docs", // /docs
);

$ch = curl_init();

// cURL options
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_URL, $url);
@curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);

@curl_setopt($ch, CURLOPT_CUSTOMREQUEST,$POST);

// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); //Log the response from HubSpot as needed.
//print_r($response);

$image_url = json_decode($response ,true);
$hub_image_url = $image_url['objects'][0]['url'];
print_r($url);


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);
?>

Thanks

View solution in original post

12 Replies 12
Teun
Guide | Diamond Partner

Hi @psama ,

 

What is the second param you pass to new CURLFile()?

Could you try setting it to 'application/octet-stream'? So:

$upload_file = new CURLFile('https://dl.airtable.com/.attachments/4b3ae7d535258081f030e2552b667693/6a41a2b5/download3.png', 'application/octet-stream');

 



Did my answer solve your issue? Help the community by marking it as the solution.
psama
Participant

Hi @Teun 
Thanks for your Response!
I have changed the content-type to 'application/octet-stream' but still I am getting the same issue.
Can you please check the below snapshot.
https://prnt.sc/1tgagib
Thanks

0 Upvotes
Teun
Guide | Diamond Partner

Hi @psama ,

 

I think you need to switch your endpoint! Check this post
Is your file URL set like this: https://dl.airtable.com/.attachments/4b3ae7d535258081f030e2552b667693/6a41a2b5/download3.png
 ?



Did my answer solve your issue? Help the community by marking it as the solution.
psama
Participant

Hi @Teun 
Thanks for the information. let me try this.
Thank you

0 Upvotes
psama
Participant

Hi @Teun 
I have checked what I found is hubspot Make some updates in hubspot files api.
they didn't released new updated hubspot post file api still,
if I am right,can you please check below snapshot and link
https://developers.hubspot.com/changelog/the-v3-files-api-is-now-available-in-public-beta
https://prnt.sc/1tgen00

Thanks

0 Upvotes
Teun
Guide | Diamond Partner

Let me double check this with @dennisedson 



Did my answer solve your issue? Help the community by marking it as the solution.
psama
Participant

Hi @Teun 
yes sure please check it.
I Need to resolve this on urjent basis.
Thanks

0 Upvotes
Teun
Guide | Diamond Partner

Hi @psama ,

 

The post you shared is from november '20. I'm pretty sure the v3 files API is out of beta. Please look at the endpoints of https://developers.hubspot.com/docs/api/files/files. I've answered a few questions about this endpoint and I am 100% positive it should work.



Did my answer solve your issue? Help the community by marking it as the solution.
psama
Participant

Hi @Teun 

Txt file is uploading to hubspot filemamnager fine using the below code.
but I want to uload the jpg, png images url,I have set "https://dl.airtable.com/.attachments/cedb4c6f3caaa6c38e130f59512af7ac/5c1c3a7e/61018d8a8b8a5.jpeg"

Then code not working. can you please look into this

Thanks

<?php
$apikey = 'my api key';
$url = "https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=" . $apikey;
//$realpath = getcwd()."\\example.txt";
$realpath = getcwd()."\\pankaj.txt";
// cURL headers
$headers = array("Content-Type:multipart/form-data");

$upload_file = new CURLFile($realpath,'application/octet-stream', 'pankaj.txt');

$file_options = array(
"access" => "PUBLIC_INDEXABLE",
"ttl" => "P3M",
"overwrite" => false,
"duplicateValidationStrategy" => "NONE",
"duplicateValidationScope" => "ENTIRE_PORTAL"
);

$post_data = array(
"file" => $upload_file,
"options" => json_encode($file_options),
"folderPath" => "/", // /docs
);

$ch = curl_init();

$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_RETURNTRANSFER => true,
);

// cURL options
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
echo '<pre>';
print_r($result);
echo '</pre>';

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);

?>

0 Upvotes
Teun
Guide | Diamond Partner

Hi @psama ,

 

Could you try setting the header to Content Type: text/plain

You should be able to upload any file type according to the docs.



Did my answer solve your issue? Help the community by marking it as the solution.
psama
Participant

Hi @Teun 
Yes, I also tried with Content Type: text/plain . but still its not working.
can you please help me in this. I need to fix it on urjent basis.

Thanks

0 Upvotes
psama
Solution
Participant

Hi @Teun 
Upload Api Works fine Now
this is working code. but I want to upload multiple images at the same time . can you give Idea about that?

<?php
$apikey = 'api key';
$url = "https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=" . $apikey;

$url2 = "https://growthnatives.com/wp-content/uploads/2020/04/logo.png";
$url3 = "https://dl.airtable.com/.attachments/c6bd26c43079ae47a60cdbb2e5e2105f/65f13185/610178d018133.png";
$pathinfo = pathinfo($url2);

$img= $pathinfo['filename'].'.'.$pathinfo['extension'];
// Function to write image into file
file_put_contents($img,file_get_contents($url2));

echo "File downloaded!";
$realpath = getcwd()."\\". $img ;

// cURL headers
$headers = array("Content-Type:multipart/form-data");
//$upload_file = new CURLFile($realpath,'application/octet-stream',$img);

$upload_file = new CURLFile($realpath,'application/octet-stream',$img);

$file_options = array(
"access" => "PUBLIC_INDEXABLE",
"ttl" => "P3M",
"overwrite" => false,
"duplicateValidationStrategy" => "NONE",
"duplicateValidationScope" => "ENTIRE_PORTAL"
);

$post_data = array(
"file" => $upload_file,
"options" => json_encode($file_options),
"folderPath" => "/docs", // /docs
);

$ch = curl_init();

// cURL options
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_URL, $url);
@curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);

@curl_setopt($ch, CURLOPT_CUSTOMREQUEST,$POST);

// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); //Log the response from HubSpot as needed.
//print_r($response);

$image_url = json_decode($response ,true);
$hub_image_url = $image_url['objects'][0]['url'];
print_r($url);


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);
?>

Thanks

View solution in original post