How to send “multipart/related” content type in PHP
<p>I need to send multipart/related content with PHP.
The content will exist of 3 files (2x XML, 1xPDF)
The XML-files need to be encoded as 7bit
The PDF-file needs to be encoded as base64</p>

<p>I can build a file like this,but then i cant figure out how to send it with curl in PHP.</p>

<p>The content should be something like this (i stripped out most of the encoded pdf). This exapmle comes from another closed-source application:</p>

<pre><code>MIME-Version: 1.0
Content-Type: multipart/related; 
    boundary="----=_Part_0_869724450.1481019442425"

------=_Part_0_869724450.1481019442425
Content-Type: application/vnd.cip4-jmf+xml; name=SubmitQueueEntry.jmf
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=SubmitQueueEntry.jmf
Content-ID: &lt;5cba3621:158d3a34526:-7ffe@hp.com&gt;
Content-Length: 465

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;JMF xmlns="http://www.CIP4.org/JDFSchema_1_1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SenderID="Alces 0.9.9.1" TimeStamp="2012-08-21T14:55:08-06:00" Version="1.3"&gt;
  &lt;Command ID="ALCES_YECIYJ_4_20120821145508" Type="SubmitQueueEntry" xsi:type="CommandSubmitQueueEntry"&gt;
    &lt;QueueSubmissionParams ReturnJMF="http://YOURHOSTNAME:9090/jmf" URL="cid:5cba3621:158d3a34526:-7fff@hp.com" /&gt;
  &lt;/Command&gt;
&lt;/JMF&gt;

------=_Part_0_869724450.1481019442425
Content-Type: application/vnd.cip4-jdf+xml; name=test.pdf.jdf
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=test.pdf.jdf
Content-ID: &lt;5cba3621:158d3a34526:-7fff@hp.com&gt;
Content-Length: 1536

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;JDF xmlns="http://www.CIP4.org/JDFSchema_1_1" Type="Combined" ID="rootNodeId" Status="Waiting" JobPartID="My Job Part ID" Version="1.3" Types="DigitalPrinting" DescriptiveName="My Job" JobID="My Job ID"&gt;
   &lt;Comment Name="JobSpec"&gt;Photobook&lt;/Comment&gt;&lt;ResourcePool&gt;
      &lt;Media Class="Consumable" ID="M001" Status="Available" /&gt;
      &lt;DigitalPrintingParams Class="Parameter" ID="DPP001" Status="Available" /&gt;
      &lt;RunList ID="RunList_1" Status="Available" Class="Parameter"&gt;
            &lt;LayoutElement&gt;
               &lt;FileSpec MimeType="application/pdf" URL="cid:5cba3621:158d3a34526:-8000@hp.com" /&gt;
            &lt;/LayoutElement&gt;
      &lt;/RunList&gt;
      &lt;Component ID="Component" ComponentType="FinalProduct" Status="Unavailable" Class="Quantity" /&gt;
   &lt;NodeInfo ID="NI001" Class="Parameter" Status="Available" LastEnd="2015-01-21T13:14:40" JobPriority="50"&gt;&lt;Comment Name="Instructions"&gt;Emboss with gold stitch&lt;/Comment&gt;&lt;GeneralID IDUsage="EmbossText" IDValue="Sara and Michael's Wedding,EmbossFontSize 20pt" /&gt;&lt;/NodeInfo&gt;&lt;CustomerInfo Class="Parameter" ID="CI001" Status="Available" /&gt;&lt;/ResourcePool&gt;
   &lt;ResourceLinkPool&gt;
      &lt;MediaLink rRef="M001" Usage="Input" /&gt;
      &lt;DigitalPrintingParamsLink rRef="DPP001" Usage="Input" /&gt;
      &lt;RunListLink rRef="RunList_1" Usage="Input" /&gt;
      &lt;ComponentLink Usage="Output" rRef="Component" Amount="1" /&gt;
   &lt;NodeInfoLink rRef="NI001" Usage="Input" /&gt;&lt;CustomerInfoLink rRef="CI001" Usage="Input" /&gt;&lt;/ResourceLinkPool&gt;
&lt;/JDF&gt;

------=_Part_0_869724450.1481019442425
Content-Type: application/octet-stream; name=_113HN_test.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=_113HN_test.pdf
Content-ID: &lt;5cba3621:158d3a34526:-8000@hp.com&gt;
Content-Length: 576230

JVBERi0xLjUNJeLjz9MNCjEgMCBvYmoNPDwvTWV0YWRhdGEgMiAwIFIvT0NQcm9wZXJ0aWVzPDwv
RDw8L09OWzUgMCBSXS9PcmRlciA2IDAgUi9SQkdyb3Vwc1tdPj4vT0NHc1s1IDAgUl0+Pi9QYWdl
cyAzIDAgUi9UeXBlL0NhdGFsb2c+Pg1lbmRvYmoNMiAwIG9iag08PC9MZW5ndGggMjcwNDIvU3Vi
dHlwZS9YTUwvVHlwZS9NZXRhZGF0YT4+c3RyZWFtDQo8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9
...
bg0KMDAwMDM2NjI5NSAwMDAwMCBuDQowMDAwMzk1NDY2IDAwMDAwIG4NCjAwMDA0MTk5MjggMDAw
MDAgbg0KdHJhaWxlcg08PC9TaXplIDQxL1Jvb3QgMSAwIFIvSW5mbyA0MCAwIFIvSURbPEM3MjlE
QzVEMUYwODQzNDA4NEY0QTlBNEJBQTE4RjhCPjxDMjU2RDIxQjA5Q0Y0MjQ4QTA5REIzRDgxNjQw
NkMzMT5dPj4Nc3RhcnR4cmVmDTQyMDEyMQ0lJUVPRg0=
------=_Part_0_869724450.1481019442425--
</code></pre>

<p>I tried the following, but it gives a empty result:</p>

<pre><code>  $url="1.2.3.4";
  $data = array('name' =&gt; basename($filePath), 'file' =&gt; '@' . realpath($filePath));
  $data = array('file' =&gt; '@' . realpath($filePath));

  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_TIMEOUT, 300);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-type: multipart/related'
    )); 
  $result = curl_exec($ch);

  $info = curl_getinfo($ch);
//  curl_close($ch);

  if ($result === false || $info['http_code'] != 200) {
    $output = "No cURL data returned for $url [" . $info['http_code'] . "]";
    if (curl_error($ch))
      $output .= "\n" . curl_error($ch);
  }
  else {
    // 'OK' status; format $output data if necessary here:
    echo 'succes';
  }
</code></pre>

<p>Maybe it whould be a better approach to build the complete content with curl-options? Instead of building the content 'manualy' before...  </p>

<p>Thanks in advance!</p>