php - 带有 php 内置 SoapClient 的附件?

标签 php soap attachment

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

最佳答案

为什么不使用 Data URI scheme 发送文件呢?而不是实现 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";

输出

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));
    }
}

关于php - 带有 php 内置 SoapClient 的附件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3120669/

相关文章:

php - 无法接收 php 文件中的 POST 数据

json - 什么是 odata、json 和 soap?

web-services - 最佳 SOAP/REST/RPC Web API 的示例?你为什么喜欢他们?他们有什么问题?

java - 通过 MIME 将多个文件附加到 Lotus Domino 中的文档

php - Laravel 4 - 将 OnChange 分配给 Form::select

php - 将 Facebook 点赞数显示为文本

php - Magento:管理员收到新订单的电子邮件

php - Nusoap "SOAP-ENV: Xml was empty, didn' t 解析"消息

php - 如何更改Wordpress附件的永久链接?

Java 邮件附件未在 Outlook 中显示