PHP SOAP获取附件
我创建了一个从SOAP下载附件的调用 调用正常,但我无法解析/保存文件 我的代码PHP SOAP获取附件,php,soap,attachment,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
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;
}
header('Content-Type:text/html;charset=utf-8');
ini\u集('soap.wsdl\u缓存\u启用',0);
ini\u集('soap.wsdl\u缓存\u ttl',0);
ini_集('memory_limit','64M');
ini设置(“最大执行时间”,1500);
$loc=”https://MyEndPoint";
$urlWSDL=$loc。“.wsdl”;
$ops=数组(
“跟踪”=>1,
“例外情况”=>0,
“压缩”=>SOAP\u compression\u ACCEPT\SOAP\u compression\u GZIP\SOAP\u compression\u DEFLATE,
'style'=>SOAP\u文档,
'use'=>SOAP\u LITERAL,
“soap_版本”=>soap_1_1,
“ResponseType”=>“Xml”,
'编码'=>'UTF-8'
);
尝试
{
$clientSOAP=newsoapclient($urlWSDL,$ops);
$clientSOAP->\u设置位置($loc);
}
捕获(e$例外){
回显“WS未激活”;
}
试一试{
$params=数组(
“fileId”=>“32213”,
“文件名”=>“Myfile.doc”
);
$result=$clientSOAP->getMyFile($params);
$strresult=$clientSOAP->uu getLastResponse();
}catch(SoapFault$异常){
回声“例外”;
echo“det.:\n”。$clientSOAP->\uu getLastResponse()。“\n”;
echo$例外;
}
调用是正确的,但我无法解析内容。
我尝试保存收到的文件有什么想法吗 最后我得到了它,我分享我的代码
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");
}
试试看{
$result=$clientSOAP->getFile($parametros);
$response=$clientSOAP->uu getLastResponse();
$data=\u条带soapheaders($response);
ECHO _parseMimeData($数据);
}捕获(SoapFault$e)
{
ECHO$e->getMessage();='看起来我们没有XML文档';
}
函数_stripSoapHeaders($response)
{
//查找xml标记的首次出现
preg_match(“/(?)/”,$response,$match);
$xml=$match['xml'];
//剥离SOAP http头和SOAP XML
$offset=strpos($response,$xml)+strlen($xml.PHP\u EOL);
返回substr($response,$offset);
}
函数_parseMimeData($data)
{
//查找MIME边界字符串
preg_match('/--(.+)\s/',$data,$match);
$mimeBoundary=$match['MIME_boundary'];//与内容相比总是唯一的
//将标题复制到客户端
if(preg_match('/(内容类型:.+?)'.PHP_EOL'/',$data,$match)){
标题($match[1]);
}
$contentType=$match[1];
if(preg_match('/(内容传输编码:.+?)'.PHP_EOL'/',$data,$match)){
标题($match[1]);
}
//从数据中删除字符串头和MIME边界
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);
返回修剪($data,“\r\n”);
}
现在的问题是将结果保存到磁盘上,我已经尝试
readfile(“files/”$data)
但我总是得到提示,而不是直接保存,我正在使用
标题('Content-Disposition:inline;filename=“files/MyFileName.doc”)
有什么想法吗?您知道如何向soap调用发送标题和正文参数吗?您知道如何向soap调用发送标题和正文参数吗?