APIs & Integrations

kishan
Participant

File upload using PHP curl post request.

Hello.

 

I am using php curl to upload file here i have code.

$url = "http://api.hubapi.com/filemanager/api/v2/files?hapikey=".$apikey;//change your api key here
      $realpath = '@'.$_FILES["file_select"]["tmp_name"].$_FILES["file_select"]["name"];
      $file_data= array('files[]'=>$realpath);
      $headers = array("Content-Type:multipart/form-data" ,  "hapikey: ".$apikey); // cURL headers for file uploading
      $postfields = array("files[]"=>$realpath,'file_names'=> $_FILES["file_select"]["name"]);
      

      $params = "--custom-boundary-123456789\r\n".
          "Content-Disposition: form-data; name=\"api\"\r\n\r\n".
          "static\r\n".
          "--custom-boundary-123456789\r\n".
          "Content-Disposition: form-data; name=\"files[]\"; filename=".$_FILES["file_select"]["name"]."\"\r\n".
          "Content-Type: text/html\r\n\r\n".
          "<div>Content for here here...</div>\r\n".
          "--custom-boundary-123456789\r\n".
          "Content-Disposition: form-data; name=\"files[]\"; filename=".$_FILES["file_select"]["name"]."\"\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='.$apikey);
      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);



      echo $reply;

and

$url = "http://api.hubapi.com/filemanager/api/v2/files?hapikey=".$apikey;//change your api key here
      $realpath = '@'.$_FILES["file_select"]["tmp_name"].$_FILES["file_select"]["name"];
      $file_data= array('files[]'=>$realpath);
      $headers = array("Content-Type:multipart/form-data" ,  "hapikey: ".$apikey); // cURL headers for file uploading
      $postfields = array("files[]"=>$realpath,'file_names'=> $_FILES["file_select"]["name"]);

$ch = curl_init();
      
      @curl_setopt($ch, CURLOPT_POST, true);
      @curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($file_data));
      @curl_setopt($ch, CURLOPT_URL, $url);
      @curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      
      @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


      $response = @curl_exec($ch);
      $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
      $curl_errors = curl_error($ch);
      @curl_close($ch);
      // echo "curl Errors: " . $curl_errors;
      // echo "\nStatus code: " . $status_code;
      // echo "\nResponse: " . $response;

      print_r($response);

Both response shows bad request.
what should i do?

0 Upvotes
1 Reply 1
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

File upload using PHP curl post request.

Hey @kishan ,

 

I'm not familiar enough with PHP to troubleshoot the code here; perhaps someone else in the community is more familiar with PHP? A quick search also returned a couple basic resources online; these may or may not be helpful to you:

 

Regardless, can you provide more detail on the error response you're seeing from HubSpot? Ideally, the status code & error message would be useful.

0 Upvotes