PHP SOAP获取附件

时间:2017-01-31 12:20:05

标签: php soap attachment

我已经创建了一个从SOAP下载附件的调用。

通话有效,但我无法解析/保存文件

我的代码

    header('Content-Type: text/html; charset=utf-8');

ini_set('soap.wsdl_cache_enable', 0 );
ini_set('soap.wsdl_cache_ttl', 0 );
ini_set('memory_limit','64M');
ini_set('max_execution_time', 1500); 

$loc        = "https://MyEndPoint";
$urlWSDL    = $loc . ".wsdl";

$ops = array(
    'trace'         => 1,
    'exceptions'    => 0,       
    'compression'   => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE,
    'style'         => SOAP_DOCUMENT,
    'use'           => SOAP_LITERAL,
    'soap_version'  => SOAP_1_1,
    "ResponseType"  => "Xml",
    'encoding'      => 'UTF-8'
);

try 
{
    $clientSOAP = new SoapClient($urlWSDL, $ops);
    $clientSOAP->__setLocation($loc);   
} 
catch (e $exception) {
        ECHO "WS not active";
}

try {
    $params = array(
            "fileId"    => "32213" ,    
            "fileName"  => "Myfile.doc"                         
    );

    $result = $clientSOAP->getMyFile($params);

    $strresult =  $clientSOAP->__getLastResponse();

} catch (SoapFault $exception) {
    echo '<h2>EXCEPTION</h2>';  
    echo "det.::\n" . $clientSOAP->__getLastResponse() . "\n";
    echo $exception;      
}   

通话正确,但我无法解析内容。 我尝试保存收到的文件
有任何想法吗?

最后我明白了,我分享了我的代码

    try {
        $result = $clientSOAP->getFile($parametros);
        $response = $clientSOAP->__getLastResponse();

        $data   = _stripSoapHeaders($response);
        ECHO _parseMimeData($data);

    } catch (SoapFault $e)
        {           
            ECHO $e->getMessage(); == 'looks like we got no XML document');
        }

function _stripSoapHeaders($response)
{
    // Find first occurance of xml tag
    preg_match('/(?<xml><.*?\?xml version=.*>)/', $response, $match);
    $xml = $match['xml'];

    // Strip SOAP http headers, and SOAP XML
    $offset = strpos($response, $xml) + strlen($xml . PHP_EOL);
    return substr($response, $offset);
}
function _parseMimeData($data)
{
    // Find MIME boundary string
    preg_match('/--(?<MIME_boundary>.+?)\s/', $data, $match);

    $mimeBoundary = $match['MIME_boundary']; // Always unique compared to content

    // Copy headers to client
    if (preg_match('/(Content-Type: .+?)'.PHP_EOL.'/', $data, $match)) {
        header($match[1]);
    }
    $contentType = $match[1];
    if (preg_match('/(Content-Transfer-Encoding: .+?)'.PHP_EOL.'/', $data, $match)) {
        header($match[1]);  
    }

    // Remove string headers and MIME boundaries from data
    preg_match('/(.*Content-ID.+'.PHP_EOL.')/', $data, $match);

    $start = strpos($data, $match[1]) + strlen($match[1]);
    $end = strpos($data, "--$mimeBoundary--");
    $data = substr($data, $start, $end-$start);

    return trim($data, "\r\n");
}   

问题是现在将结果保存在磁盘上,我试过 ReadFile的(“文件/".$数据);

但是我总是被提示,而不是直接保存,我正在使用

header('Content-Disposition:inline; filename =“files / MyFileName.doc”');

有什么想法吗?

0 个答案:

没有答案