简体   繁体   中英

cURL upload in PHP to Hubspot / “no file found”

I tried to upload .pdf files from my personal CRM to Hubspot root folder with PHP :

$url = "http://api.hubapi.com/filemanager/api/v2/files?hapikey=".$hapikey;
$realpath = "http://localhost:8888/steps/D_201803_14122.pdf";
$headers = array("Content-Type:multipart/form-data"); 
$postfields = array("files"=>$realpath,'file_names'=>'D_201803_14122FROMPHP.pdf');
$ch = curl_init();
$options = array(
  CURLOPT_URL => $url,
  CURLOPT_HEADER => true,
  CURLOPT_POST => 1,
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_POSTFIELDS => $postfields,
  CURLOPT_RETURNTRANSFER => true
); 
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); 

And I get this error :

{"status":"error","message":"no file found, please set name to 'files' or 'files[]'","correlationId":"d6ae2bcc-c26d-48db-91a9-d601cea56e7e","requestId":"5364051dc0127abf389ecf56f185f994"}

The pdf file is in the same folder as the php script.

I tried with different paths like :

$realpath = "D_201803_14122.pdf";
$realpath = '@'."D_201803_14122.pdf";

Still the same error

What's wrong with this piece of code ?

Thanks.

You can add files to the request with curl_file_create()

$realpath should be the local path and not the url. Something like getcwd() . "/D_201803_14122.pdf should work if the script is in the same directory like the file.

$url = "http://api.hubapi.com/filemanager/api/v2/files?hapikey=".$hapikey;
$realpath = getcwd() . "/D_201803_14122.pdf";
$headers = array("Content-Type:multipart/form-data"); 
$postfields = array("files"=>curl_file_create($realpath),'file_names'=>'D_201803_14122FROMPHP.pdf');
$ch = curl_init();
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM