2012-09-13 2 views
0

xml에 cURL을 사용하여 캐나다 게시물에 접속하고 배송 레이블을 얻습니다.오프셋 37에 잘못된 문자 'X'가 표시됩니다.

이것은 내가 사용하는 코드입니다.

플랫폼은 내가

아래 오류가 발생

<?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"; 
     } 
    } 
} 

?> 

ExpressionEngine이다 HTTP 응답 상태 : 500 오류 코드 : 서버 오류 메시지 : 잘못된 문자 'X'로 (37)의 오프셋 (offset)/R을/0000000000/0000000000/배송

(고객 번호를 "0000000000"으로 변경)

누군가 위의 메시지의 의미를 설명 할 수 있습니까?

당신에게 대단히 감사합니다

+0

HTTP 응답 상태 500은 일반적으로 내부 서버 오류 (서버 쪽이 엉망입니다)를 의미합니다. 또한 캐나다 게시물 오류 메시지를 살펴 보았습니다. http://www.canadapost.ca/cpo/mc/business/productsservices/developers/messagescodetables.jsf "500"코드가 없습니다. 아마도 CURL의 일부가 끝날 때 가장자리 오류를 일으키는 것일 수 있습니다. – Shad

+0

'$ xmlRequest'에서'mb_detect_encoding'을 실행하면 출력되는 내용은 무엇입니까? (아마도 당신은 잘못된 형식의 UTF8을 보내고있을 것입니다.) – Shad

+0

친애하는 Shad, 답장을 보내 주셔서 대단히 감사합니다. mb_detect_encoding을 실행할 때 "ASCII Error"메시지가 나타납니다. 이 문제를 해결하도록 안내해주세요. 감사합니다 – user1427195

답변

0

공급자는 아마 (모든 "기업"공급자 등)이 아닌 적절한 XML 파서를 사용하여. PI의 닫는 문자 앞에 공백을 넣거나 실패한 경우 PI를 완전히 제거하십시오.

+0

친애하는 Ignacio vazquez-Abrams, "PI의 인사를 끝내는 것"은 무엇을 의미합니까? 답장을 위해 대단히 감사합니다. – user1427195

+0

처리 명령 끝에 '?>'. –

+0

고맙습니다. 나는 그것을 시도 할 것이다. – user1427195

0

37 문자 바로 프롤로그가

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

어느 호스트가이를 처리하지 않는 XML의 끝에서 당신을두고 또는 당신은 DOS/UNIX 줄 끝 문제가있을 수 있습니다.

먼저 XML 프롤로그를 제거하고 도움이되는지 확인하십시오.

그래도 도움이되지 않는다면, 편집자에 따라 PHP 소스를 UNIX 파일로 저장하여 라인 마커의 끝을 올바르게 가져 오십시오. 그래도 작동하지 않으면 DOS 파일로 저장하십시오.

+0

답장을 보내 주셔서 대단히 감사합니다. 나는 너의 재촉을 시도 할 것이다 – user1427195

+0

나는 언급하는 것을 잊었다. ExpressionEngine에서 코드를 사용합니다. – user1427195