Good day,
I am attempting to try to build my own request using SimpleXMLElement but keep getting timeout.
I am successful when using other method.
Below I share both files content fail and success.
Can you help me fix the problem?
Thank You
Success
<html>
<head>
<title>test</title>
</head>
<body>
<?php
$url = 'http://sellonline.canadapost.ca:30000';
$xml = '<?xml version="1.0" ?>
<eparcel>
<language>en</language>
<ratesAndServicesRequest>
<merchantCPCID> CPC_DEMO_XML </merchantCPCID>
<fromPostalCode>m1p1c0</fromPostalCode>
<turnAroundTime> 24 </turnAroundTime>
<itemsPrice>0.00</itemsPrice>
<lineItems>
<item>
<quantity> 1 </quantity>
<weight> 1.491 </weight>
<length> 1 </length>
<width> 1 </width>
<height> 1 </height>
<description> KAO Diskettes </description>
<readyToShip/>
</item>
</lineItems>
<city> </city>
<provOrState> Wisconsin </provOrState>
<country> CANADA </country>
<postalCode> H3K1E5</postalCode>
</ratesAndServicesRequest>
</eparcel>';
$ch = curl_init($url); // Create REST Request
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
$curl_response = curl_exec($ch); // Execute REST Request
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch) . '\n';
}
echo 'HTTP Response Status: ' . curl_getinfo($ch,CURLINFO_HTTP_CODE) . '<br />';
curl_close($ch);
// Example of using SimpleXML to parse xml response
libxml_use_internal_errors(true);
$xml = simplexml_load_string($curl_response);
if (!$xml) {
echo "Failed loading XML\n";
echo "$curl_response\n";
foreach (libxml_get_errors() as $error) {
echo "\t" . $error->message;
}
} else {
if ($xml->{'ratesAndServicesResponse'} ) {
$ratesAndServicesResponse = $xml->{'ratesAndServicesResponse'};
echo "<center><font color='black'><h3>Status Code " . $ratesAndServicesResponse->{"statusCode"} . "</font></h3></center>";
echo "<center><font color='black'><h3>Status Message " . $ratesAndServicesResponse->{"statusMessage"} . "</font></h3></center>";
echo "<center><font color='black'><h3>request ID " . $ratesAndServicesResponse->{"requestID"} . "</font></h3></center>";
echo "<center><font color='black'><h3>handling " . $ratesAndServicesResponse->{"handling"} . "</font></h3></center>";
echo "<center><font color='black'><h3>language " . $ratesAndServicesResponse->{"language"} . "</font></h3></center>";
if ($ratesAndServicesResponse->{'product'} ) {
foreach ($ratesAndServicesResponse->{'product'} as $Product){
//echo "<center><font color='black'><h3>Sequence " . $Product->{'sequence'}. "</font></h3></center>";
echo "<center><font color='black'><h3>Product: " . $Product->{'name'}. "</font></h3></center>";
echo "<center><font color='black'><h3>Price: " . $Product->{'rate'} . "</font></h3></center>";
echo "<center><font color='black'><h3>Shipping Date: " . $Product->{'shippingDate'} . "</font></h3></center>";
echo "<center><font color='black'><h3>Delivery Date: " . $Product->{'deliveryDate'} . "</font></h3></center>";
echo "<center><font color='black'><h3>delivery Day Of Week: " . $Product->{'deliveryDayOfWeek'} . "</font></h3></center>";
echo "<center><font color='black'><h3>next Day AM: " . $Product->{'nextDayAM'} . "</font></h3></center>";
echo "<center><font color='black'><h3>packingID: " . $Product->{'packingID'} . "</font></h3></center>";
}
foreach ($ratesAndServicesResponse->{'packing'} as $packing){
echo "<center><font color='black'><h3>packingID: " . $packing->{'packingID'} . "</font></h3></center>";
}
foreach ($ratesAndServicesResponse->{'packing'}->{'box'} as $box){
echo "<center><font color='black'><h3>Name: " . $box->{'name'} . "</font></h3></center>";
echo "<center><font color='black'><h3>Weight: " . $box->{'weight'} . "</font></h3></center>";
// echo "<center><font color='black'><h3>expediterWeight: " . $box->{'expediterWeight'} . "</font></h3></center>";
echo "<center><font color='black'><h3>length: " . $box->{'length'} . "</font></h3></center>";
echo "<center><font color='black'><h3>width: " . $box->{'width'} . "</font></h3></center>";
echo "<center><font color='black'><h3>height: " . $box->{'height'} . "</font></h3></center>";
echo "<center><font color='black'><h3>quantity: " . $box->{'packedItem'}->{'quantity'} . "</font></h3></center>";
echo "<center><font color='black'><h3>description: " . $box->{'packedItem'}->{'description'} . "</font></h3></center>";
}
foreach ($ratesAndServicesResponse->{'shippingOptions'} as $shippingOptions){
echo "<center><font color='black'><h3>insurance: " . $shippingOptions->{'insurance'} . "</font></h3></center>";
echo "<center><font color='black'><h3>deliveryConfirmation: " . $shippingOptions->{'deliveryConfirmation'} . "</font></h3></center>";
echo "<center><font color='black'><h3>signature: " . $shippingOptions->{'signature'} . "</font></h3></center>";
}
}
foreach ($ratesAndServicesResponse->{'nearestPostalOutlet'} as $nearestPostalOutlet){
echo "<center><font color='black'><h3>postalOutletSequenceNo: " . $nearestPostalOutlet->{'postalOutletSequenceNo'} . "</font></h3></center>";
echo "<center><font color='black'><h3>distance: " . $nearestPostalOutlet->{'distance'} . "</font></h3></center>";
echo "<center><font color='black'><h3>outletName: " . $nearestPostalOutlet->{'outletName'} . "</font></h3></center>";
echo "<center><font color='black'><h3>businessName: " . $nearestPostalOutlet->{'businessName'} . "</font></h3></center>";
echo "<center><font color='black'><h3>addressLine: " . $nearestPostalOutlet->{'postalAddress'}->{'addressLine'} . "</font></h3></center>";
echo "<center><font color='black'><h3>postalCode: " . $nearestPostalOutlet->{'postalAddress'}->{'postalCode'} . "</font></h3></center>";
echo "<center><font color='black'><h3>municipality: " . $nearestPostalOutlet->{'postalAddress'}->{'municipality'} . "</font></h3></center>";
echo "<center><font color='black'><h3>province: " . $nearestPostalOutlet->{'postalAddress'}->{'province'} . "</font></h3></center>";
echo "<center><font color='black'><h3>phoneNumber: " . $nearestPostalOutlet->{'phoneNumber'} . "</font></h3></center>";
foreach ($ratesAndServicesResponse->{'nearestPostalOutlet'}->{'businessHours'} as $businessHours){
echo "<center><font color='black'><h3>dayId: " . $businessHours->{'dayId'} . "</font></h3></center>";
echo "<center><font color='black'><h3>dayOfWeek: " . $businessHours->{'dayOfWeek'} . "</font></h3></center>";
echo "<center><font color='black'><h3>time: " . $businessHours->{'time'} . "</font></h3></center>";
}
}
}else {
$messages = 'Why?';
foreach ($messages as $message) {
echo "<font color ='white'>Error Code:" . $message->code . "</font>\n";
echo "<font color ='white'>Error Msg:" . $message->description . "</font>\n\n";
}
}
}
// print_r($xml);
?>
</body>
</html>
TIMEOUT
<html>
<head>
<title>test</title>
</head>
<body>
<?php
$url = 'http://sellonline.canadapost.ca:30000';
$xml = new SimpleXMLElement('<eparcel></eparcel>');
$xml->addChild('language', htmlspecialchars(utf8_encode(' en ')));
$ratesAndServicesRequest = $xml->addChild('ratesAndServicesRequest');
$ratesAndServicesRequest->addChild('merchantCPCID', htmlspecialchars(utf8_encode(' CPC_DEMO_HTML ')));
$ratesAndServicesRequest->addChild('fromPostalCode', htmlspecialchars(utf8_encode(' k2k0b1 ')));
$ratesAndServicesRequest->addChild('turnAroundTime', htmlspecialchars(utf8_encode(' 24 ')));
$ratesAndServicesRequest->addChild('itemsPrice', htmlspecialchars(utf8_encode(' 100.00 ')));
$lineItems = $ratesAndServicesRequest->addChild('lineItems');
$items = $lineItems->addChild('item');
$items->addChild('quantity', htmlspecialchars(utf8_encode(' 1 ')));
$items->addChild('weight', htmlspecialchars(utf8_encode(' 1.491 ')));
$items->addChild('length', htmlspecialchars(utf8_encode(' 1 ')));
$items->addChild('width', htmlspecialchars(utf8_encode(' 1 ')));
$items->addChild('height', htmlspecialchars(utf8_encode(' 1 ')));
$items->addChild('description', htmlspecialchars(utf8_encode(' description ')));
$items->addChild('readyToShip');
$ratesAndServicesRequest->addChild('city', htmlspecialchars(utf8_encode(' City ')));
$ratesAndServicesRequest->addChild('provOrState', htmlspecialchars(utf8_encode(' Provine ')));
$ratesAndServicesRequest->addChild('country', htmlspecialchars(utf8_encode(' CANADA ')));
$ratesAndServicesRequest->addChild('postalCode', htmlspecialchars(utf8_encode(' K0A1W0 ')));
echo "XML web service request </br>";
echo "<textarea>";
echo '<hr />';
echo $xml->asXML();
echo '<hr />';
echo "</textarea>";
$ch = curl_init($url); // Create REST Request
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
$curl_response = curl_exec($ch); // Execute REST Request
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch) . '\n';
}
echo 'HTTP Response Status: ' . curl_getinfo($ch,CURLINFO_HTTP_CODE) . '<br />';
curl_close($ch);
// Example of using SimpleXML to parse xml response
libxml_use_internal_errors(true);
$xml = simplexml_load_string($curl_response);
if (!$xml) {
echo "Failed loading XML\n";
echo "$curl_response\n";
foreach (libxml_get_errors() as $error) {
echo "\t" . $error->message;
}
} else {
if ($xml->{'ratesAndServicesResponse'} ) {
$ratesAndServicesResponse = $xml->{'ratesAndServicesResponse'};
echo "<center><font color='black'><h3>Status Code " . $ratesAndServicesResponse->{"statusCode"} . "</font></h3></center>";
echo "<center><font color='black'><h3>Status Message " . $ratesAndServicesResponse->{"statusMessage"} . "</font></h3></center>";
echo "<center><font color='black'><h3>request ID " . $ratesAndServicesResponse->{"requestID"} . "</font></h3></center>";
echo "<center><font color='black'><h3>handling " . $ratesAndServicesResponse->{"handling"} . "</font></h3></center>";
echo "<center><font color='black'><h3>language " . $ratesAndServicesResponse->{"language"} . "</font></h3></center>";
if ($ratesAndServicesResponse->{'product'} ) {
foreach ($ratesAndServicesResponse->{'product'} as $Product){
echo "<center><font color='black'><h3>Product: " . $Product->{'name'}. "</font></h3></center>";
echo "<center><font color='black'><h3>Price: " . $Product->{'rate'} . "</font></h3></center>";
echo "<center><font color='black'><h3>Shipping Date: " . $Product->{'shippingDate'} . "</font></h3></center>";
echo "<center><font color='black'><h3>Delivery Date: " . $Product->{'deliveryDate'} . "</font></h3></center>";
echo "<center><font color='black'><h3>delivery Day Of Week: " . $Product->{'deliveryDayOfWeek'} . "</font></h3></center>";
echo "<center><font color='black'><h3>next Day AM: " . $Product->{'nextDayAM'} . "</font></h3></center>";
echo "<center><font color='black'><h3>packingID: " . $Product->{'packingID'} . "</font></h3></center>";
}
foreach ($ratesAndServicesResponse->{'packing'} as $packing){
echo "<center><font color='black'><h3>packingID: " . $packing->{'packingID'} . "</font></h3></center>";
}
foreach ($ratesAndServicesResponse->{'packing'}->{'box'} as $box){
echo "<center><font color='black'><h3>Name: " . $box->{'name'} . "</font></h3></center>";
echo "<center><font color='black'><h3>Weight: " . $box->{'weight'} . "</font></h3></center>";
echo "<center><font color='black'><h3>length: " . $box->{'length'} . "</font></h3></center>";
echo "<center><font color='black'><h3>width: " . $box->{'width'} . "</font></h3></center>";
echo "<center><font color='black'><h3>height: " . $box->{'height'} . "</font></h3></center>";
echo "<center><font color='black'><h3>quantity: " . $box->{'packedItem'}->{'quantity'} . "</font></h3></center>";
echo "<center><font color='black'><h3>description: " . $box->{'packedItem'}->{'description'} . "</font></h3></center>";
}
foreach ($ratesAndServicesResponse->{'shippingOptions'} as $shippingOptions){
echo "<center><font color='black'><h3>insurance: " . $shippingOptions->{'insurance'} . "</font></h3></center>";
echo "<center><font color='black'><h3>deliveryConfirmation: " . $shippingOptions->{'deliveryConfirmation'} . "</font></h3></center>";
echo "<center><font color='black'><h3>signature: " . $shippingOptions->{'signature'} . "</font></h3></center>";
}
}
foreach ($ratesAndServicesResponse->{'nearestPostalOutlet'} as $nearestPostalOutlet){
echo "<center><font color='black'><h3>postalOutletSequenceNo: " . $nearestPostalOutlet->{'postalOutletSequenceNo'} . "</font></h3></center>";
echo "<center><font color='black'><h3>distance: " . $nearestPostalOutlet->{'distance'} . "</font></h3></center>";
echo "<center><font color='black'><h3>outletName: " . $nearestPostalOutlet->{'outletName'} . "</font></h3></center>";
echo "<center><font color='black'><h3>businessName: " . $nearestPostalOutlet->{'businessName'} . "</font></h3></center>";
echo "<center><font color='black'><h3>addressLine: " . $nearestPostalOutlet->{'postalAddress'}->{'addressLine'} . "</font></h3></center>";
echo "<center><font color='black'><h3>postalCode: " . $nearestPostalOutlet->{'postalAddress'}->{'postalCode'} . "</font></h3></center>";
echo "<center><font color='black'><h3>municipality: " . $nearestPostalOutlet->{'postalAddress'}->{'municipality'} . "</font></h3></center>";
echo "<center><font color='black'><h3>province: " . $nearestPostalOutlet->{'postalAddress'}->{'province'} . "</font></h3></center>";
echo "<center><font color='black'><h3>phoneNumber: " . $nearestPostalOutlet->{'phoneNumber'} . "</font></h3></center>";
foreach ($ratesAndServicesResponse->{'nearestPostalOutlet'}->{'businessHours'} as $businessHours){
echo "<center><font color='black'><h3>dayId: " . $businessHours->{'dayId'} . "</font></h3></center>";
echo "<center><font color='black'><h3>dayOfWeek: " . $businessHours->{'dayOfWeek'} . "</font></h3></center>";
echo "<center><font color='black'><h3>time: " . $businessHours->{'time'} . "</font></h3></center>";
}
}
}else {
echo "Why?";
}
}
print_r($xml);
?>
</body>
</html>