有没有办法可以使用
PHP的内置SoapClient类为请求添加soap附件?它看起来不像是支持,但也许我可以手动构建mime边界?我知道PEAR SOAP库支持它们,但是为了使用它我必须重写我的整个库来使用它.
为什么不用
Data URI scheme发送文件而不是实现
SoapAttachment?这是一个例子:
原文链接:https://www.f2er.com/php/136748.html客户
$client = new SoapClient(null,array( 'location' => "http://localhost/lab/stackoverflow/a.PHP?h=none",'uri' => "http://localhost/",'trace' => 1 )); // Method 1 Array // File to upload $file = "golf3.png"; // First Example $data = array(); $data['name'] = $file; $data['data'] = getDataURI($file,"image/png"); echo "Example 1: "; echo ($return = $client->upload($data)) ? "File Uploaded : $return bytes" : "Error Uploading Files"; // Method 2 Objects // File to upload $file = "original.png"; // Second Example $attachment = new ImageObj($file); $param = new SoapVar($attachment,SOAP_ENC_OBJECT,"ImageObj"); $param = new SoapParam($param,"param"); echo "Example 2: "; echo ($return = $client->uploadObj($attachment)) ? "File Uploaded : $return bytes" : "Error Uploading Files";
产量
Example 1: File Uploaded : 976182 bytes Example 2: File Uploaded : 233821 bytes
服务器
class UploadService { public function upload($args) { $file = __DIR__ . "/test/" . $args['name']; return file_put_contents($file,file_get_contents($args['data'])); } public function uploadObj($args) { $file = __DIR__ . "/test/" . $args->name; $data = sprintf("data://%s;%s,%s",$args->mime,$args->encoding,$args->data); return file_put_contents($file,file_get_contents($data)); } } try { $server = new SOAPServer(NULL,array( 'uri' => 'http://localhost/' )); $server->setClass('UploadService'); $server->handle(); } catch (SOAPFault $f) { print $f->faultstring; }
客户端工具
// Function Used function getDataURI($image,$mime = '') { return 'data: ' . (function_exists('mime_content_type') ? mime_content_type($image) : $mime) . ';base64,' . base64_encode(file_get_contents($image)); } // Simple Image Object class ImageObj{ function __construct($file,$mime = "") { $this->file = $file; $this->name = basename($file); if (function_exists('mime_content_type')) { $this->mime = mime_content_type($file); } elseif (function_exists('finfo_open')) { $this->mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE),$file); } else { $this->mime = $mime; } $this->encoding = "base64"; $this->data = base64_encode(file_get_contents($file)); } }