附带php的内置SoapClient?

Har*_*83- 8 php soap attachment

有没有办法可以使用PHP的内置SoapClient类为请求添加soap附件?看起来它不支持,但也许我可以手动构建mime边界?我知道PEAR SOAP库支持它们,但是为了使用它我必须重写我的整个库来使用它.

Bab*_*aba 5

为什么不使用数据URI方案发送文件而不是实现SoapAttachment?这是一个例子:

客户

$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";
Run Code Online (Sandbox Code Playgroud)

产量

Example 1: File Uploaded : 976182 bytes
Example 2: File Uploaded : 233821 bytes
Run Code Online (Sandbox Code Playgroud)

服务器

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;
}
Run Code Online (Sandbox Code Playgroud)

客户端工具

// 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));
    }
}
Run Code Online (Sandbox Code Playgroud)