用Regex来获取MTOM二进制PDF内容--新闻中心

问题描述 投票:0回答:1

我试图使用SoapClient的扩展类来获取MTOM的二进制内容(这是一个PDF)。 这是一个参考类。https:/github.comdebussMTOMSoapClientblobmasterMTOMSoapClient.php。

所以,如果你看一下这个类,你可以看到,我收到一个响应,然后我使用regex来获得它的特定部分。这是我得到的整个响应。

整个响应

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: <[email protected]>

<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:[email protected]"/></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: <[email protected]>

%PDF-1.4
...

   LOTS OF BINARY CODE HERE FOR THE PDF

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


xml响应

<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:[email protected]" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
            </contentFile>
         </return>
      </ns2:downloadAttachResponse>
   </soap:Body>
</soap:Envelope>

使用该类的代码,我在二进制数组上得到一个错误,该数组基本为空,因为regex没有得到pdf内容。这是具体的部分。

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

那么,如何才能用regex得到PDF二进制内容呢?

谢谢!我正在尝试使用SoapClient的扩展类来获取MTOM的二进制内容(即PDF)。

php regex binary soap-client mtom
1个回答
0
投票

在示例数据中,Content-ID:部分以""结束。>

Content-ID: <[email protected]>

在你使用的模式中,这部分 $cid.'[\s\S].+?>要求至少有一个字符 [\s\S] 后面的1次以上的任意字符非贪婪的。.+? 这意味着,在 >

但在示例数据中,它们之间没有字符。

你可以更新代码,使用

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

Regex演示

如果数据总是这样的结构,你可能会优化一下模式。

Content-ID:\h+<[email protected]>\R\s*((?:(?!--uuid).*\R)*)--uuid

Regex演示

© www.soinside.com 2019 - 2024. All rights reserved.