Note that there are some explanatory texts on larger screens.

plurals
  1. POillegal character 'X' at offset 37
    primarykey
    data
    text
    <p>I am using xml with cURL to contact Canada post and get shipping labels.</p> <p>This is the code I use.</p> <p><strong>The platform is ExpressionEngine</strong></p> <pre><code>&lt;?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 = &lt;&lt;&lt;XML &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;shipment xmlns="http://www.canadapost.ca/ws/shipment"&gt; &lt;group-id&gt;{$groupId}&lt;/group-id&gt; &lt;requested-shipping-point&gt;{$requestedShippingPoint}&lt;/requested-shipping-point&gt; &lt;expected-mailing-date&gt;{$mailingDate}&lt;/expected-mailing-date&gt; &lt;delivery-spec&gt; &lt;service-code&gt;DOM.EP&lt;/service-code&gt; &lt;sender&gt; &lt;name&gt;Bulma&lt;/name&gt; &lt;company&gt;Capsule Corp.&lt;/company&gt; &lt;contact-phone&gt;1 (514) 820 5879&lt;/contact-phone&gt; &lt;address-details&gt; &lt;address-line-1&gt;502 MAIN ST N&lt;/address-line-1&gt; &lt;city&gt;MONTREAL&lt;/city&gt; &lt;prov-state&gt;QC&lt;/prov-state&gt; &lt;country-code&gt;CA&lt;/country-code&gt; &lt;postal-zip-code&gt;H2B1A0&lt;/postal-zip-code&gt; &lt;/address-details&gt; &lt;/sender&gt; &lt;destination&gt; &lt;name&gt;Ryuko Saito&lt;/name&gt; &lt;company&gt;Kubere&lt;/company&gt; &lt;address-details&gt; &lt;address-line-1&gt;23 jardin private&lt;/address-line-1&gt; &lt;city&gt;Ottawa&lt;/city&gt; &lt;prov-state&gt;ON&lt;/prov-state&gt; &lt;country-code&gt;CA&lt;/country-code&gt; &lt;postal-zip-code&gt;K1K4T3&lt;/postal-zip-code&gt; &lt;/address-details&gt; &lt;/destination&gt; &lt;options&gt; &lt;option&gt; &lt;option-code&gt;DC&lt;/option-code&gt; &lt;/option&gt; &lt;/options&gt; &lt;parcel-characteristics&gt; &lt;weight&gt;15&lt;/weight&gt; &lt;dimensions&gt; &lt;length&gt;6&lt;/length&gt; &lt;width&gt;12&lt;/width&gt; &lt;height&gt;9&lt;/height&gt; &lt;/dimensions&gt; &lt;unpackaged&gt;true&lt;/unpackaged&gt; &lt;mailing-tube&gt;false&lt;/mailing-tube&gt; &lt;/parcel-characteristics&gt; &lt;notification&gt; &lt;email&gt;ryuko.saito@kubere.com&lt;/email&gt; &lt;on-shipment&gt;true&lt;/on-shipment&gt; &lt;on-exception&gt;false&lt;/on-exception&gt; &lt;on-delivery&gt;true&lt;/on-delivery&gt; &lt;/notification&gt; &lt;print-preferences&gt; &lt;output-format&gt;8.5x11&lt;/output-format&gt; &lt;/print-preferences&gt; &lt;preferences&gt; &lt;show-packing-instructions&gt;true&lt;/show-packing-instructions&gt; &lt;show-postage-rate&gt;false&lt;/show-postage-rate&gt; &lt;show-insured-value&gt;true&lt;/show-insured-value&gt; &lt;/preferences&gt; &lt;settlement-info&gt; &lt;contract-id&gt;{$contractId}&lt;/contract-id&gt; &lt;intended-method-of-payment&gt;Account&lt;/intended-method-of-payment&gt; &lt;/settlement-info&gt; &lt;/delivery-spec&gt; &lt;/shipment&gt; 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('&lt;root&gt;' . preg_replace('/&lt;\?xml.*\?&gt;/','',$curl_response) . '&lt;/root&gt;'); if (!$xml) { echo 'Failed loading XML' . "\n"; echo $curl_response . "\n"; foreach(libxml_get_errors() as $error) { echo "\t" . $error-&gt;message; } } else { if ($xml-&gt;{'shipment-info'} ) { $shipment = $xml-&gt;{'shipment-info'}-&gt;children('http://www.canadapost.ca/ws/shipment'); if ( $shipment-&gt;{'shipment-id'} ) { echo 'Shipment Id: ' . $shipment-&gt;{'shipment-id'} . "\n"; foreach ( $shipment-&gt;{'links'}-&gt;{'link'} as $link ) { echo $link-&gt;attributes()-&gt;{'rel'} . ': ' . $link-&gt;attributes()-&gt;{'href'} . "\n"; } } } if ($xml-&gt;{'messages'} ) { $messages = $xml-&gt;{'messages'}-&gt;children('http://www.canadapost.ca/ws/messages'); foreach ( $messages as $message ) { echo 'Error Code: ' . $message-&gt;code . "\n"; echo 'Error Msg: ' . $message-&gt;description . "\n\n"; } } } ?&gt; </code></pre> <p><strong>I received error below</strong></p> <p>HTTP Response Status: 500 Error Code: Server Error Msg: illegal character 'X' at offset 37 of /rs/0000000000/0000000000/shipment </p> <p>(I changed customer number to "0000000000")</p> <p>Can someone explain what is the meaning of above message?</p> <p>Thank you very much</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload