用php打印变量的gif

时间:2011-08-30 05:57:16

标签: php xml

我正在试验Endicia运输标签服务器。下面的示例代码允许我从他们的测试服务器获取USPS标签。如何显示正在返回的图像。现在print_r函数(如果未注释掉)会打印出看似图像的数组内容。

<?php

$strGetLabelURL = "https://www.envmgr.com/LabelService/EwsLabelService.asmx/GetPostageLabelXML";

$request = '<LabelRequest ImageFormat="GIF" Test="YES">
     <RequesterID>abcd</RequesterID>
     <AccountID>123456</AccountID>
     <PassPhrase>samplePassPhrase</PassPhrase>
     <MailClass>FIRST</MailClass>
     <DateAdvance>0</DateAdvance>
     <WeightOz>1</WeightOz>
     <Stealth>FALSE</Stealth>
     <Services InsuredMail="OFF" SignatureConfirmation="OFF" />
     <Value>0</Value>
     <Description>Sample Label</Description>
     <PartnerCustomerID>12345ABCD</PartnerCustomerID>
     <PartnerTransactionID>6789EFGH</PartnerTransactionID>
     <ToName>Ben Franklin</ToName>
     <ToCompany>United States Postal Service</ToCompany>
     <ToAddress1>12345 Main Street</ToAddress1>
     <ToCity>Camas</ToCity>
     <ToState>WA</ToState>
     <ToPostalCode>98607</ToPostalCode>
     <ToPhone>2025551212</ToPhone>
     <FromName>Technical Support</FromName>
     <FromCompany>DYMO Endicia</FromCompany>
     <ReturnAddress1>385 Sherman Ave.</ReturnAddress1>
     <FromCity>Palo Alto</FromCity>
     <FromState>CA</FromState>
     <FromPostalCode>94306</FromPostalCode>
     <FromZIP4>1864</FromZIP4>
     <FromPhone>8005763279</FromPhone>
     </LabelRequest>';

 $params = array('http' => array(
     'method' => 'POST',
     'content' => 'labelRequestXML='.$request,
     'header' => 'Content-Type: application/x-www-form-urlencoded'));

 $ctx = stream_context_create($params);
 $fp = fopen($strGetLabelURL, 'rb', false, $ctx);

 if (!$fp) 
 {
     print "Problem with $strGetLabelURL";
 }

 $response = stream_get_contents($fp);

 if ($response === false) 
 {
     print "Problem reading data from $url, $php_errormsg";
 }

 print_r($response);
?>

2 个答案:

答案 0 :(得分:7)

您必须加载XML,提取图像数据,然后将其放入图像中:

$sxml = Simplexml_load_string( $response );
echo '<img src="data:image/gif;base64,' . $sxml->Base64LabelImage . '">';

答案 1 :(得分:2)

我不知道Endicia解决方案,但我认为它与UPS非常相似。 从您发送的XML中可以看到您要求GIF格式的标签。 我想在响应中你有一个名为<LabelImage>的元素或类似的元素。您需要提取开始和结束标记之间的值,并使用下面的内容在浏览器上打印它:

echo '<img src="data:image/gif;base64,' . $value . '" alt="" />';