0

私はWSO2WSFrameworkを使用しており、Webサービスが画像をMTOM添付ファイルとして返し、file_put_contents(...)コマンドを使用してクライアントによって保存される例を実行することができました。

サービス:

<?php

function sendAttachment($msg){
$responsePayloadString = <<<XML
    <ns1:download xmlns:ns1="http://wso2.org/wsfphp/samples/mtom">
        <ns1:fileName>test.jpg</ns1:fileName>
            <ns1:image xmlmime:contentType="image/jpeg" xmlns:xmlmime="http://www.w3.org/2004/06/xmlmime">
                <xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:myid1"></xop:Include>
            </ns1:image>
    </ns1:download>
XML;
$f = file_get_contents("test.jpg");                                        

$responseMessage = new WSMessage($responsePayloadString, 
        array( "attachments" => array("myid1" => $f)));  
return $responseMessage;    
}

$operations = array("download" => "sendAttachment");

$service = new WSService(array("operations" => $operations, "useMTOM" => TRUE));

$service->reply();

?>

クライアント:

<?php

$requestPayloadString = '<download></download>';

try {

$client = new WSClient(
    array( "to" => "http://SPLINTER/MTOM/service.php",
           "useMTOM" => TRUE,
           "responseXOP" => TRUE));

$requestMessage = new WSMessage($requestPayloadString);                    
$responseMessage = $client->request($requestMessage);

printf("Response = %s \n", $responseMessage->str);

$cid2stringMap = $responseMessage->attachments;
$cid2contentMap = $responseMessage->cid2contentType;
$imageName;
if($cid2stringMap && $cid2contentMap){
    foreach($cid2stringMap as $i=>$value){
        $f = $cid2stringMap[$i];
        $contentType = $cid2contentMap[$i];
        if(strcmp($contentType,"image/jpeg") ==0){
            $imageName = $i."."."jpg";
            if(stristr(PHP_OS, 'WIN')) {
                file_put_contents($imageName, $f);
            }else{
                file_put_contents("/tmp/".$imageName, $f);
            }
        }
    }
}else{
    printf("attachments not received ");
}

} catch (Exception $e) {

if ($e instanceof WSFault) {
    printf("Soap Fault: %s\n", $e->Reason);
} else {
    printf("Message = %s\n",$e->getMessage());
}
}
?>

その代わりに、「保存ダイアログ」を開いて、ファイルを開くか保存するかを選択したいと思います。解決策を探すとき、私は次のようなヘダーの設定について読みました:

header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename="test.jpg"');

しかし、それはうまくいきませんでした。「保存ダイアログ」がポップアップしましたが、ファイルが空であると言って画像を開くことができなかったとき。

実際、このMTOMアタッチメントがどのように機能しているかはよくわかりません。クライアントコードでは、$ fは文字列だと思いますが、printf($ f)を実行すると、0(ゼロ)が出力されるので、この文字列を画像として保存するにはどうすればよいですか?

4

1 に答える 1

0

そのヘッダーを使用する場合は、ファイルの内容を出力する必要があり、どこかに保存する必要はありません。

header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename="test.jpg"');

// Output file. This must be the ONLY output of the whole script
echo $rawFileContents;

基本は、ファイルのコンテンツ全体を変数にロードした時点で(そしてそれは$fコードにあるように見えます)、ファイルに書き込む代わりに出力するということです(私が今やっていると思います)。file_put_contents()したがって、私が提供した3行のコードは、呼び出しを置き換える必要があります。

代わりに、ファイルを/tmpフォルダに保存する場合は、それを実行しますが、代わりに使用します

header('Location: /tmp/' . $imageName);

このようにして、ユーザーのブラウザーを保存されたファイルに直接リダイレクトし、ユーザーがそれを使ってやりたいことを実行できるようにします。

于 2012-06-06T14:05:00.767 に答える