2012-03-23 13 views
5

मैं जावा एसओएपी सेवा से एसडब्ल्यूए प्रतिक्रियाओं को संभालने का प्रयास कर रहा हूं। उस एसडब्ल्यूए प्रतिक्रिया में कुछ एमआईएमई हेडर के साथ एक्सएमएल के अंत में एक द्विआधारी लगाव लगाया जाता है। मैं निर्भरता आवश्यकता सीमाओं के लिए डब्ल्यूएसओ 2 का उपयोग नहीं कर सकता।PHP - एसडब्ल्यूए को संभालने के लिए सोप क्लाइंट को विस्तारित करना (अटैचमेंट के साथ एसओएपी)

किसी भी मदद की बहुत सराहना की जाएगी!

// Input 

------=_Part_42_539586119.1332526191981 
Content-Type: text/xml; charset=UTF-8 
Content-Transfer-Encoding: binary 
Content-Id: <03B4708A9544C182C43E51D9ADA1E456> 

<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body> ... TRUNCATED XML SOAP RESPONSE ... </soapenv:Body></soapenv:Envelope> 

------=_Part_42_539586119.1332526191981 
Content-Type: image/png 
Content-Transfer-Encoding: binary 
Content-Id: <D637B1257E3E5EEA06AF0E45494F8448> 

BINARY DATA GOES HERE 


// End of input 

// स्क्रिप्ट प्रतिक्रिया विभाजित + StdObj की वापसी माता पिता वर्ग SoapClient + तरह फ़ाइलें

namespace Project; 
class SoapClient extends \SoapClient 
{ 
    public function __call ($function_name, $arguments) 
    { 
     // have the parent do a soap call, catch the lastResponse() if an error 
     // occurred (eg has an attachment) and parse it out. 
     try { 
      $r = parent::__call($function_name, $arguments); 
      return $r; 
     } catch (\Exception $e) { 
      // Assumption: When this is sent, it means that a file is being sent 
      // because SimpleXML can't process it. 
      if ($e->getMessage() == "looks like we got no XML document") { 
       $response = parent::__getLastResponse(); 
       $partString = "/(------=_[a-zA-Z0-9_\\.]+)/"; 
       $outputArr = preg_split($partString, $response); 
       // $outputAtt[0] -- empty and is the first MIME Part Header 
       // $outputArr[1] -- Mime Header + XML (The SOAP Response) 
       // $outputArr[n+1] -- additional files w/ MIME headers 
       if (array_key_exists(1, $outputArr)) { 
        // remove the first 5 lines (4 MIME Header lines) + 1 Blank 
        // line 
        $data = implode("\n", 
        array_slice(explode("\n", $outputArr[1]), 5)); 

        /// Simple XML Object ... appears to be an empty SimpleXMLElement though ... >:-(
        $xml = simplexml_load_string($data, null, null, "http://schemas.xmlsoap.org/soap/envelope/"); 




       } else { 
        // OK Maybe this doesn't actually contain the XML... throw 
        // the original exception. 
        throw new \SoapFault($e->getMessage(), $e->getCode(), 
        $e->getPrevious()); 
       } 
      } else { 
       throw new \SoapFault($e->getMessage(), $e->getCode(), 
       $e->getPrevious()); 
      } 
     } 
    } 
} 
+0

आपको अधिक जानकारी प्रदान करनी चाहिए, मुझे समझ में नहीं आ रहा है कि समस्या कहां है। क्या ये भाग शायद एन्कोड किए गए हैं? क्या यह वैध एक्सएमएल है? क्या आपने इसे पार्स किया था? क्या आपने [libxml_get_errors()] (http://php.net/manual/function.libxml-get-errors.php) के साथ कुछ त्रुटि लॉगिंग करने का प्रयास किया था? –

+0

यह हेडर से द्विआधारी अनुलग्नक को पकड़ने के लिए एक दिलचस्प हैक-स्टाइल प्रयास है। मैं थोड़ी देर के लिए भी इस दिशा में आगे बढ़ गया है। –

+0

आपको अपने आप पर regex के बजाय, पेलोड को पार्स करने के लिए एक [MIME पार्सर एक्सटेंशन] (https://code.google.com/p/php-mime-mail-parser/) का उपयोग करने का प्रयास करना चाहिए। – quickshiftin

उत्तर

4

उपयोग इस के लिए एक MIME parser साथ कुछ करने के लिए। आप और अधिक ठीक से the spec लागू करना चाहते हैं, तो आप सोप एक्सएमएल के खिलाफ माइम संलग्नक अप मैच के लिए की आवश्यकता होगी

require_once('MimeMailParser.class.php'); 

class SwADownloadSoapClient extends SoapClient 
{ 
    const ATTACHMENT_DIR = '/path/to/saved/attachments/'; 

    public function __doRequest(
     $request, $location, $action, $version, $one_way=0 
    ) { 
     // Issue the SOAP request as SoapClient would normall 
     $sResult = parent::__doRequest(
           $request, $location, $action, $version, $one_way); 

     // Handle and parse MIME-encoded messages 
     // @note We're not doing much inspection 
     //  of the XML payload against the attachments ATM 
     //  so not sure how greatly this lives up to the spec 
     $sResult = $this->_parseMimeMessage($sResult); 

     $oParser = new MimeMailParser(); 
     $oParser->setText($sResult); 

     // Save the attachments 
     $aAttachments = $oParser->getAttachments(); 
     foreach($aAttachments as $oAttachment) { 
      $sFile = $oAttachment->filename; 
      if($rFp = fopen(self::ATTACHMENT_DIR . $sFile, 'w')) { 
       while($sBytes = $attachment->read()) 
        fwrite($rFp, $sBytes); 
       fclose($rFp); 
      } 
     } 
    } 
} 

: यह वहाँ से बहुत आसान है।

+0

मैं [ब्लॉग किया गया] (http://quickshiftin.com/blog/2013/09/soap-client-attachments-php/) डाउनलोड और अपलोड दोनों के समाधान के बारे में। – quickshiftin

संबंधित मुद्दे