2012-09-13 91 views
0

我使用xml和cURL联系加拿大邮政和 获取运输标签。非法字符'X'在偏移量37

这是我使用的代码。

该平台是ExpressionEngine

<?php 
/** 
* Sample code for the CreateShipment Canada Post service. 
* 
* The CreateShipment service is used to create a new shipping item, to 
* request the generation of a softcopy image of shipping labels, and to provide 
* links to these shipping labels and other information associated with the 
* shipping item.. 
* 
* This sample is configured to access the Developer Program sandbox environment. 
* Use your development key username and password for the web service credentials. 
* 
**/ 

// Your username, password and customer number are imported from the following file  
// CPCWS_Shipping_PHP_Samples\REST\shipping\user.ini 
$userProperties = parse_ini_file(realpath(dirname($_SERVER['SCRIPT_FILENAME'])) . '/../user.ini'); 

$username = $userProperties['username']; 
$password = $userProperties['password']; 
$mailedBy = $userProperties['customerNumber']; 
$mobo = $userProperties['customerNumber']; 

// REST URL 
$service_url = 'https://ct.soa-gw.canadapost.ca/rs/' . $mailedBy . '/' . $mobo . '/shipment'; 

// Create CreateShipment request xml 
$groupId = '4326432'; 
$requestedShippingPoint = 'H2B1A0'; 
$mailingDate = '2012-10-24'; 
$contractId = '0040662521'; 

$xmlRequest = <<<XML 
<?xml version="1.0" encoding="UTF-8"?> 
<shipment xmlns="http://www.canadapost.ca/ws/shipment"> 
    <group-id>{$groupId}</group-id> 
    <requested-shipping-point>{$requestedShippingPoint}</requested-shipping-point> 
    <expected-mailing-date>{$mailingDate}</expected-mailing-date> 
    <delivery-spec> 
     <service-code>DOM.EP</service-code> 
      <sender> 
       <name>Bulma</name> 
       <company>Capsule Corp.</company> 
       <contact-phone>1 (514) 820 5879</contact-phone> 
       <address-details> 
        <address-line-1>502 MAIN ST N</address-line-1> 
        <city>MONTREAL</city> 
        <prov-state>QC</prov-state> 
        <country-code>CA</country-code> 
        <postal-zip-code>H2B1A0</postal-zip-code> 
       </address-details> 
      </sender> 
      <destination> 
       <name>Ryuko Saito</name> 
       <company>Kubere</company> 
       <address-details> 
        <address-line-1>23 jardin private</address-line-1> 
        <city>Ottawa</city> 
        <prov-state>ON</prov-state> 
        <country-code>CA</country-code> 
        <postal-zip-code>K1K4T3</postal-zip-code> 
       </address-details> 
      </destination> 
     <options> 
      <option> 
       <option-code>DC</option-code> 
      </option> 
     </options> 
     <parcel-characteristics> 
      <weight>15</weight> 
      <dimensions> 
       <length>6</length> 
       <width>12</width> 
       <height>9</height> 
      </dimensions> 
      <unpackaged>true</unpackaged> 
      <mailing-tube>false</mailing-tube> 
     </parcel-characteristics> 
     <notification> 
      <email>[email protected]</email> 
      <on-shipment>true</on-shipment> 
      <on-exception>false</on-exception> 
      <on-delivery>true</on-delivery> 
     </notification> 
     <print-preferences> 
      <output-format>8.5x11</output-format> 
     </print-preferences> 
     <preferences> 
      <show-packing-instructions>true</show-packing-instructions> 
      <show-postage-rate>false</show-postage-rate> 
      <show-insured-value>true</show-insured-value> 
     </preferences> 
     <settlement-info> 
      <contract-id>{$contractId}</contract-id> 
      <intended-method-of-payment>Account</intended-method-of-payment> 
     </settlement-info> 
    </delivery-spec> 
</shipment> 
XML; 

$curl = curl_init($service_url); // Create REST Request 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($curl, CURLOPT_CAINFO, realpath(dirname($argv[0])) . '/../../../third-party/cert/cacert.pem'); // Signer Certificate in PEM format 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlRequest); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($curl, CURLOPT_USERPWD, $username . ':' . $password); 
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/vnd.cpc.shipment-v2+xml', 'Accept: application/vnd.cpc.shipment-v2+xml')); 
$curl_response = curl_exec($curl); // Execute REST Request 
if(curl_errno($curl)){ 
    echo 'Curl error: ' . curl_error($curl) . "\n"; 
} 

echo 'HTTP Response Status: ' . curl_getinfo($curl,CURLINFO_HTTP_CODE) . "\n"; 

curl_close($curl); 

// Example of using SimpleXML to parse xml response 
libxml_use_internal_errors(true); 
$xml = simplexml_load_string('<root>' . preg_replace('/<\?xml.*\?>/','',$curl_response) . '</root>'); 
if (!$xml) { 
    echo 'Failed loading XML' . "\n"; 
    echo $curl_response . "\n"; 
    foreach(libxml_get_errors() as $error) { 
     echo "\t" . $error->message; 
    } 
} else { 
    if ($xml->{'shipment-info'}) { 
     $shipment = $xml->{'shipment-info'}->children('http://www.canadapost.ca/ws/shipment'); 
     if ($shipment->{'shipment-id'}) { 
      echo 'Shipment Id: ' . $shipment->{'shipment-id'} . "\n";     
      foreach ($shipment->{'links'}->{'link'} as $link) { 
       echo $link->attributes()->{'rel'} . ': ' . $link->attributes()->{'href'} . "\n"; 
      } 
     } 
    } 
    if ($xml->{'messages'}) {     
     $messages = $xml->{'messages'}->children('http://www.canadapost.ca/ws/messages');  
     foreach ($messages as $message) { 
      echo 'Error Code: ' . $message->code . "\n"; 
      echo 'Error Msg: ' . $message->description . "\n\n"; 
     } 
    } 
} 

?> 

我接收到下面

错误HTTP响应状态:500错误代码:服务器错误消息:非法字符 'X' 的偏移量37的/ RS/0000000000/0000000000 /出货

(我将客户号码更改为“0000000000”)

有人可以解释上面的消息是什么意思吗?

非常感谢您

+0

500的HTTP响应状态通常意味着内部服务器错误(服务器端出现了一些问题)。 此外,我看着加拿大员额错误消息: http://www.canadapost.ca/cpo/mc/business/productsservices/developers/messagescodetables.jsf 而且没有“500”代码。也许你的CURL中的某些东西会导致边缘大小写错误。 – Shad

+0

如果您在'$ xmlRequest'上运行'mb_detect_encoding',输出是什么? (也许你发送畸形的UTF8) – Shad

+0

亲爱的谢德,非常感谢你的回复。当我运行mb_detect_encoding时,出现“ASCII Error”消息。请指导我解决这个问题。谢谢 – user1427195

回答

0

提供者可能是(像所有“企业”供应商)没有使用正确的XML解析器。尝试在PI的结尾字符之前放置一个空格,否则将无法完全删除PI。

+0

亲爱的Ignacio vazquez-Abrams,您是什么意思“关闭PI的字符”非常感谢您的回复 – user1427195

+0

处理指令结尾的'?>'。 –

+0

非常感谢。我会尝试。 – user1427195

0

37个字符让您身在XML结束序言

<?xml version="1.0" encoding="UTF-8"?> 

无论是主机不处理,或者你有可能有DOS/UNIX最终的线的问题。

首先,尝试删除XML Prolog,看看是否有帮助。

如果这没有帮助,那么(取决于您的编辑器)将PHP源文件保存为UNIX文件,以使行标记结束。如果这不起作用,请尝试将其保存为DOS文件。

+0

非常感谢您的回复。我会尝试你的推荐 – user1427195

+0

我忘了提。我使用ExpressionEngine上的代码。 – user1427195

相关问题