2011-08-30 33 views
5

Tôi đang thử nghiệm với máy chủ nhãn vận chuyển Endicia. Mã mẫu bên dưới cho phép tôi lấy nhãn USPS từ máy chủ thử nghiệm của họ. Làm thế nào tôi sẽ hiển thị hình ảnh đang được trả lại. Ngay bây giờ chức năng print_r (nếu không nhận xét) sẽ in ra nội dung mảng của những gì có vẻ là một hình ảnh.In gif từ một biến với php

<?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); 
?> 
+0

Nội dung của $ response là gì? – xdazz

+0

Ảnh của blob có phải là phần tử mảng không? – alex

Trả lời

7

Bạn cần phải nạp XML, trích xuất các dữ liệu hình ảnh, sau đó đặt nó trong hình ảnh:

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

Tuyệt vời ... cảm ơn. Tôi đã thêm các tùy chọn bổ sung để ngăn chặn các cảnh báo ... Simplexml_load_string ($ response, NULL, LIBXML_NOWARNING); và nó hoạt động rất tốt. Cảm ơn. – RThomas

2

Tôi không biết về giải pháp Endicia nhưng tôi nghĩ nó khá giống với UPS. Từ XML bạn gửi có thể thấy rằng bạn yêu cầu nhãn ở định dạng GIF. Tôi cho rằng trong phản hồi bạn có một phần tử có tên là <LabelImage> hoặc một cái gì đó tương tự. Bạn cần phải trích xuất các giá trị đó là giữa mở và thẻ đóng và sử dụng dưới đây để in nó trên trình duyệt của bạn:

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