phpregexbinarysoap-clientmtom

Regex to get MTOM binary PDF content


I am trying to get the MTOM binary content (which is a PDF) using an extended class of SoapClient.
This is the reference class: https://github.com/debuss/MTOMSoapClient/blob/master/MTOMSoapClient.php

So if you take a look at the class, you can see that I receive a response and then I use regex to get specific parts of it. This is the entire response I get.

entire response

HTTP/1.1 200 OK
Date: ...
Server: .....
Set-Cookie: ....; Path=/; HttpOnly
Set-Cookie: ...; Path=/; HttpOnly
Content-Type: multipart/related; type="application/xop+xml"; boundary="uuid:0f7fa750-c317-4039-897a-a90685b00d29"; start="<...>"; start-info="text/xml"
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked

--uuid:0f7fa750-c317-4039-897a-a90685b00d29
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml";
Content-Transfer-Encoding: binary
Content-ID: <root.message@cxf.apache.org>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:downloadAttachResponse xmlns:ns2="http://..../"><return><errStr></errStr><result>0</result><contentFile><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:c4d149e6-3f6a-4aa8-bd07-244c92bdc262-2@cxf.apache.org"/></contentFile></return></ns2:downloadAttachResponse></soap:Body></soap:Envelope>
--uuid:0f7fa750-c317-4039-897a-a90685b00d29
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <c4d149e6-3f6a-4aa8-bd07-244c92bdc262-2@cxf.apache.org>

%PDF-1.4
...

   LOTS OF BINARY CODE HERE FOR THE PDF

--uuid:0f7fa750-c317-4039-897a-a90685b00d29--"



xml response

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:downloadAttachResponse xmlns:ns2="http://.../">
         <return>
            <errStr/>
            <result>0</result>
            <contentFile>
               <xop:Include href="cid:c4d149e6-3f6a-4aa8-bd07-244c92bdc262-2@cxf.apache.org" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
            </contentFile>
         </return>
      </ns2:downloadAttachResponse>
   </soap:Body>
</soap:Envelope>


Using the code of the class, I get an error on the binary array which is basically empty because the regex doesn't get the pdf content. This is the specific part:

regex


// Get CID
$cid = null;
preg_match('/cid:([0-9a-zA-Z-]+)@/', $xop_element, $cid);
$cid = $cid[1];

// Get Binary
$binary = null;
preg_match('/Content-ID:[\s\S].+?'.$cid.'[\s\S].+?>([\s\S]*?)--uuid/', $response, $binary);
$binary = trim($binary[1]);   // error here



So, how can I get the PDF binary content using regex?

Thanks!


Solution

  • In the example data, the Content-ID: part ends with a >

    Content-ID: <c4d149e6-3f6a-4aa8-bd07-244c92bdc262-2@cxf.apache.org>
    

    In the pattern that you use, this parts $cid.'[\s\S].+?>expects at least a single character [\s\S] followed by 1+ times any character non greedy .+? which means that there should be at least 2 characters before the >

    But in the example data, there are no chars between them.

    You could update the code to use

    preg_match('/Content-ID:[\s\S].+?'.$cid.'[\s\S]*?>([\s\S]*?)--uuid/', $response, $binary);
    

    Regex demo

    If the data is always structured like that, you might optimize the pattern a bit:

    Content-ID:\h+<c4d149e6-3f6a-4aa8-bd07-244c92bdc262-2@cxf.apache.org>\R\s*((?:(?!--uuid).*\R)*)--uuid
    

    Regex demo