Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
Dear Experts,

I am trying to send address information to Canada Post using xml code with PHP

When ever I send data I get this error message:

HTTP Response Status: 400 Error Code: 1623 Error Msg: A name or address field contains an invalid character such as |.

This is a data I am trying to send $shipping_city = "St. Philip's";

This is the xml code I use:

<pre lang="PHP"><pre lang="PHP">$xmlRequest = <<&lt;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>ahhahah</name>
                <company>hahha Importers</company>
                <contact-phone>613 611 1112</contact-phone>
                <address-details>
                    <address-line-1>1501 Liiight Ave. Bay 7</address-line-1>
                    <city>OTTAWA</city>
                    <prov-state>ON</prov-state>
                    <country-code>CA</country-code>
                    <postal-zip-code>K1T1M5</postal-zip-code>
                </address-details>
            </sender>
            <destination>
                <name>$name</name>
                    <address-details>
                    <address-line-1>$address1</address-line-1>
                    <address-line-2>$address2</address-line-2>
                    <city>$shipping_city</city>
                    <prov-state>$shipping_state</prov-state>
                    <country-code>$shipping_country_code</country-code>
                    <postal-zip-code>$shipping_zip</postal-zip-code>
                </address-details>
            </destination>
        &lt;options>
            &lt;option>
                &lt;option-code>DC&lt;/option-code>
            &lt;/option>
        &lt;/options>
        <parcel-characteristics>
            <weight>0.4</weight>
            <dimensions>
                <length>6</length>
                <width>3</width>
                <height>3</height>
            </dimensions>
            <unpackaged>false</unpackaged>
            <mailing-tube>false</mailing-tube>
        </parcel-characteristics>
        <notification>
            <email>$email</email>
            <on-shipment>true</on-shipment>
            <on-exception>false</on-exception>
            <on-delivery>true</on-delivery>
        </notification>
        <print-preferences>
            <output-format>4x6</output-format>
        </print-preferences>
        <preferences>
            <show-packing-instructions>false</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])) . '/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);
</pre>



Can someone guide me to stop the error message and send data to Canada post

Thank you very much
Posted
Updated 2-Oct-12 17:05pm
v2
Comments
Sergey Alexandrovich Kryukov 2-Oct-12 22:59pm    
Yes... it looks like Canada post system has hallucination... I wonder if they could have some stupid bug.
Did you try to send more realistic name, for an attempt of a workaround? Seriously, it could be some stupid bug which reports your "ahhahah" in a weird way. What if it checks for proper capitalization or something like that?

The fact that the server-side software is pretty bad is visible with a unarmed eye. For example, reasonable software won't tell you "a name or address field", it will tell what exactly field it was.
--SA
Sergey Alexandrovich Kryukov 2-Oct-12 23:07pm    
However, it could be $address1, $address2 or something. Please output all variables (you don't tell us the values) to check them up. It could be another source of the bug, in this case -- yours.
--SA
Member 9478973 2-Oct-12 23:10pm    
Dear SA, Thank you very much for your comments. I am sorry, I may be did not explain my issue. "ahhahah" is just a name I put to avoid putting my company personal's name. Error show up because of $shipping_city data has a '(apostrophe) Do you know a way to include apostrophe in $shipping_city data and send it to Canada Post. Thank you very much again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900