开发者

Printing a gif from a variable with php

开发者 https://www.devze.com 2023-03-31 11:02 出处:网络
I am experimenting with the Endicia shipping label server.The sample code below allows me to get a USPS label from their test server.How would I display the image that is being returned.Right now the

I am experimenting with the Endicia shipping label server. The sample code below allows me to get a USPS label from their test server. How would I display the image that is being returned. Right now the print_r function (if un-commented out) does print out the array contents of what appears to be an image.

<?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<开发者_如何学C;/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);
?>


You have to load the XML, extract the image data, then put it in an image:

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


I don't know about Endicia solution but I think it's quite similar to UPS. From the XML you send one can see that you ask for the label in GIF format. I suppose that in the response you have an element named <LabelImage> or something similar. You need to extract the value that is between the opening and the closing tag and use below to print it on your browser:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消