title: PHP Post Request with CURL - image + array
body: <p>I'm trying to post a multipart request to an API that contains both an image and an array that has details about that image. This request works via Postman but implementing it using CURL in PHP is proving difficult.</p>
  
  <p>The headers are pretty basic - they just contain an $auth_token that is retrieved previously from other api calls.</p>
  
  <pre><code>$headers = array('Content-Type: multipart/form-data', 'Authorization-token: ' . $auth_token));
  </code></pre>
  
  <p>The actual body of the form data is as follows - a CurlFile is instantiated as follows and an array with information about the file </p>
  
  <pre><code>$fields = array(
      "data" =&gt; new CurlFile('C://images/15.jpg', 'image/jpg', '15.jpg'),
      "entityFile" =&gt; array(
          "fileTypeID"=&gt; "server generated id",
          "fileSize"=&gt; 23453,
          "entityID"=&gt; "another server generated id",
          "fileDateUtc"=&gt; "2018-02-26T00:00:00",
          "fileName"=&gt; "15",
          "fileExtension"=&gt; "jpg"
      )
  );
  </code></pre>
  
  <p>Finally I set options for the data to be sent</p>
  
  <pre><code>$ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FAILONERROR, true);
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  $result = curl_exec($ch);
  </code></pre>
  
  <p>I have also tried attaching the image as a base64 encoded string but the image on the server is not changed.</p>
  
  <p>My question is whether there is anything apparently wrong with my $fields array that might be causing it to fail? I suspect that there may be a problem with the actual file but it is hard to determine as the actual error returned is...</p>
  
  <pre><code>The requested URL returned error: 400 Bad Request
  </code></pre>
  
  <p>Thank you</p>