PHP HTTP(POSR/GET)示例代码


使用PHP HTTP(POSR/GET)方式来获取国际包裹相关信息,可以使用PHP的DOM、simplexml等XML处理类或函数来处理返回的数据。在这里我们主要是使用DOM来解析XML。

 

查询ISHIP邮寄国家名称

在提交运费查询之前,用户须先获取ISHIP系统的国家名称,以供选择。

代码:
<?php
//HTTP REQUIRE URL
define('REQUIRECOUNTRYURL','http://www.sendfromchina.com/shipfee/country_list');
?>
<p>Get sfcservice.com shipping rate.</p>
<form action="XMLGetRateRespones.php" method="post">

<table cellpadding="0">
<tr>
<td>Country</td><td>
<select name="countryName">
<?php

$doc = new DOMDocument();
$doc->load( REQUIRECOUNTRYURL );
$dataset = $doc->getElementsByTagName( "country" );
foreach( $dataset as $row ){
$countryNames = $row->getElementsByTagName( "englishname" );
$countryName = $countryNames->item(0)->nodeValue;
?>

<option value='<?php echo $countryName ?>'><?php echo $countryName ?></option>
<?php }?>
</select>
</td> </tr>
</table>
</form>


 


 

查询ISHIP支持的邮寄方式

代码:

<?php
//HTTP REQUIRE URL
define('REQUIRESHIPTYPE','http://www.sendfromchina.com/shipfee/ship_type_list');
?>
<p>Get sfcservice.com shipping rate.</p>
<form action="XMLGetRateRespones.php" method="post">

<table cellpadding="0">
<tr>
<td>Ship Class</td><td>
<select name="shipmode">
<?php

$doc = new DOMDocument();
$doc->load( REQUIRESHIPTYPE );
$dataArr = $doc->getElementsByTagName( "shiptype" );
foreach( $dataArr as $row ){
$en_names = $row->getElementsByTagName( "en_name" );
$en_name = $en_names->item(0)->nodeValue;
$method_codes = $row->getElementsByTagName( "method_code" );
$method_code = $method_codes->item(0)->nodeValue;
?>

<option value='<?php echo $method_code ?>'><?php echo $en_name ?></option>
<?php }?>
</select>
</td> </tr>
</table>
</form>



 

查询ISHIP支持的邮寄类型

代码:

<?php
//HTTP REQUIRE URL
define('REQUIRESHIPCLASS','http://www.sendfromchina.com/shipfee/ship_class_list');
?>
<p>Get sfcservice.com shipping rate.</p>

<table cellpadding="0">
<tr>
<td>Ship Type</td><td>
<select name="classType">
<?php

$doc = new DOMDocument();
$doc->load( REQUIRESHIPCLASS );
$dataArr = $doc->getElementsByTagName( "shipclass" );
foreach( $dataArr as $row ){
$en_names = $row->getElementsByTagName( "en_name" );
$en_name = $en_names->item(0)->nodeValue;
$class_codes = $row->getElementsByTagName( "class_code" );
$class_code = $class_codes->item(0)->nodeValue;
?>
<option value='<?php echo $class_code ?>'><?php echo $en_name ?></option>
<?php }?>
</select>
</td> </tr>
</table>



 

直接查询可用运费规则

此查询需要提交国际包裹的目的国家与重量。所以在提交查询时,我们先生成一个表单来取得国家信息与货物重量,再提交查询。

提交查询表单代码:

<?php
define('REQUIRECOUNTRYURL','http://www.sendfromchina.com/shipfee/country_list');
?>
<p>Get sfcservice.com shipping rate.</p>
<form action="getRateRespones.php" method="post">

<table cellpadding="0">
<tr>
<td>Country</td><td>
<select name="countryName">
<?php

$doc = new DOMDocument();
$doc->load( REQUIRECOUNTRYURL );
$dataset = $doc->getElementsByTagName( "country" );
foreach( $dataset as $row ){
$countryNames = $row->getElementsByTagName( "englishname" );
$countryName = $countryNames->item(0)->nodeValue;
?>

<option value='<?php echo $countryName ?>'><?php echo $countryName ?></option>
<?php }?>
</select>
</td> </tr>
<tr>
<td>Weight:</td>
<td><input type="text" name="weight" /></td>

</tr> <tr>
<td colspan="2"><input type="submit" value="GetRates" /></td>
</tr>
</table>
</form>



getRateRespones.php代码:

<?php
// URL to call
define('REQUIRERATE','http://www.sendfromchina.com/shipfee/out_rates');
$countryName = @$_POST['countryName'];
$weight = @$_POST['weight'];

// Construct the GetShippingRate call
$profile = '?country='.$countryName.'&weight='.$weight;

require_once 'XMLGetRate.php';

$doc = new DOMDocument();
$doc->load( REQUIRERATE .$profile);
// Load the call and capture the document returned by sendfromchina.com API
$dataset = $doc->getElementsByTagName( "rate" );
// Check to see if the response was loaded, else print an error
if(@$dataset->item(0)->nodeValue){
?>

<hr color="#6794B8" size="2" >
<table cellspacing="1" cellpadding="0" width="85%">
<tr align="center" bgcolor="#C3D5FD">

<th width="30%">Mail Service</th>
<th width="25%">Total Fee(&#165;CNY)</th>

<th width="25%">Delivery time(Workday)</th>
<th width="10%">Tracking</th>
<th width="10%">By volume</th>
</tr>
<?php
// For each Shippinh Rate Item node, and display all of them
foreach( $dataset as $row ){
$totalfees = $row->getElementsByTagName( "totalfee" );
$totalfee = $totalfees->item(0)->nodeValue;
$deliverytimes = $row->getElementsByTagName( "deliverytime" );
$deliverytime = $deliverytimes->item(0)->nodeValue;
$iftrackings = $row->getElementsByTagName( "iftracking" );
$iftracking = $iftrackings->item(0)->nodeValue;
$classtypes = $row->getElementsByTagName( "classtype" );
$classtype = $classtypes->item(0)->nodeValue;
$byvolumes = $row->getElementsByTagName( "byvolume" );
$byvolume = $byvolumes->item(0)->nodeValue;
$shiptypes = $row->getElementsByTagName( "shiptypename" );
$shiptypename = $shiptypes->item(0)->nodeValue;
?>

<tr align="center" bgcolor="#ffffff">
<td width="30%"><?php echo $shiptypename ?></td>

<td width="25%"><?php echo $totalfee ?></td>

<td width="25%"><?php echo $deliverytime ?></td>
<td width="10%"><?php echo $iftracking ?></td>
<td width="10%"><?php echo $byvolume ?></td>
</tr>

<?php
}

?> </table>
<?php
}else{
//display error message
$errors = $doc->getElementsByTagName( "errors" );
$error = $errors->item(0)->nodeValue;
echo $error;

}

?>

 

按邮寄类型查询运费规则

此查询需要提交国际包裹的目的国家、重量与类型。在提交查询前,我们应生成一个表单来取得国家信息、货物重量与类型,再提交查询。

提交查询表单代码:
<?php
define('REQUIRECOUNTRYURL','http://www.sendfromchina.com/shipfee/country_list');
define('REQUIRESHIPCLASS','http://www.sendfromchina.com/shipfee/ship_class_list');
?>
<p>Get sfcservice.com shipping rate by type.</p>
<form action="getRateByTypeRespones.php" method="post">
<table cellpadding="0">
<tr>
<td>Country</td><td>
<select name="countryName">
<?php
$doc = new DOMDocument();
$doc->load( REQUIRECOUNTRYURL );
$dataset = $doc->getElementsByTagName( "country" ); //获取国家
foreach( $dataset as $row ){
$countryNames = $row->getElementsByTagName( "englishname" );
$countryName = $countryNames->item(0)->nodeValue;
?>

<option value='<?php echo $countryName ?>'><?php echo $countryName ?></option>
<?php
}
unset($doc);

?>
</select>
</td> </tr>
<tr>
<td>Type</td><td>
<select name="type">
<?php
$doc = new DOMDocument();
$doc->load( REQUIRESHIPCLASS );
$dataArr = $doc->getElementsByTagName( "shipclass" ); //获取运费类型
foreach( $dataArr as $row ){
$en_names = $row->getElementsByTagName( "en_name" );
$en_name = $en_names->item(0)->nodeValue;
$class_codes = $row->getElementsByTagName( "class_code" );
$class_code = $class_codes->item(0)->nodeValue;
?>
<option value='<?php echo $class_code ?>'><?php echo $en_name ?></option>
<?php
}
unset($doc);
?>
</select>
</td> </tr>
<tr>
<td>Weight:</td>
<td><input type="text" name="weight" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="GetRates" /></td>
</tr>
</table>
</form>
getRateByTypeRespones.php代码:

<?php
// URL to call
define('REQUIRERATE','http://www.sendfromchina.com/shipfee/out_rates');
$countryName = @$_POST['countryName'];
$weight = @$_POST['weight'];

// Construct the GetShippingRate call
$profile = '?country='.$countryName.'&weight='.$weight;
//Construct the GetShippingRate By type
$profile .= '&type='.$_POST['type'];
require_once 'XMLGetRateByType.php';
$doc = new DOMDocument();
$doc->load( REQUIRERATE .$profile);
// Load the call and capture the document returned by sendfromchina.com API
$dataset = $doc->getElementsByTagName( "rate" );
// Check to see if the response was loaded, else print an error
if(@$dataset->item(0)->nodeValue){
?>

<hr color="#6794B8" size="2" >
<table cellspacing="1" cellpadding="0" width="85%">
<tr align="center" bgcolor="#C3D5FD">

<th width="30%">Mail Service</th>
<th width="25%">Total Fee(&#165;CNY)</th>

<th width="25%">Delivery time(Workday)</th>
<th width="10%">Tracking</th>
<th width="10%">By volume</th>
</tr>
<?php
// For each Shippinh Rate Item node, and display all of them
foreach( $dataset as $row ){
$totalfees = $row->getElementsByTagName( "totalfee" );
$totalfee = $totalfees->item(0)->nodeValue;
$deliverytimes = $row->getElementsByTagName( "deliverytime" );
$deliverytime = $deliverytimes->item(0)->nodeValue;
$iftrackings = $row->getElementsByTagName( "iftracking" );
$iftracking = $iftrackings->item(0)->nodeValue;
$classtypes = $row->getElementsByTagName( "classtype" );
$classtype = $classtypes->item(0)->nodeValue;
$byvolumes = $row->getElementsByTagName( "byvolume" );
$byvolume = $byvolumes->item(0)->nodeValue;
$shiptypes = $row->getElementsByTagName( "shiptypename" );
$shiptypename = $shiptypes->item(0)->nodeValue;
?>

<tr align="center" bgcolor="#ffffff">
<td width="30%"><?php echo $shiptypename ?></td>

<td width="25%"><?php echo $totalfee ?></td>

<td width="25%"><?php echo $deliverytime ?></td>
<td width="10%"><?php echo $iftracking ?></td>
<td width="10%"><?php echo $byvolume ?></td>
</tr>

<?php
}

?>

</table>
<?php
}else{
//display error message
$errors = $doc->getElementsByTagName( "errors" );
$error = $errors->item(0)->nodeValue;
echo $error;

}

?>

 

按邮寄方式查询运费规则

此查询需要提交国际包裹的目的国家、重量与邮寄方式。在提交查询前,我们应生成一个表单来取得国家信息、货物重量与邮寄方式,再提交查询。

提交查询表单代码:

<?php
//HTTP REQUIRE URL
define('REQUIRECOUNTRYURL','http://www.sendfromchina.com/shipfee/country_list');
define('REQUIRESHIPTYPE','http://www.sendfromchina.com/shipfee/ship_type_list');
?>

<p>Get sfcservice.com shipping rate by mode.</p>
<form action="getRateByTypeRespones.php" method="post">

<table cellpadding="0">
<tr>
<td>Country:</td><td>
<select name="countryName">
<?php

$doc = new DOMDocument();
$doc->load( REQUIRECOUNTRYURL );
$dataset = $doc->getElementsByTagName( "country" );
foreach( $dataset as $row ){
$countryNames = $row->getElementsByTagName( "englishname" );
$countryName = $countryNames->item(0)->nodeValue;
?>

<option value='<?php echo $countryName ?>'><?php echo $countryName ?></option>
<?php }?>
</select>
</td> </tr>
<tr>
<td>Mode:</td><td>
<select name="mode">
<?php

$doc = new DOMDocument();
$doc->load( REQUIRESHIPTYPE );
$dataArr = $doc->getElementsByTagName( "shiptype" );
foreach( $dataArr as $row ){
$en_names = $row->getElementsByTagName( "en_name" );
$en_name = $en_names->item(0)->nodeValue;
$method_codes = $row->getElementsByTagName( "method_code" );
$method_code = $method_codes->item(0)->nodeValue;
?>
<option value='<?php echo $method_code ?>'><?php echo $en_name ?></option>
<?php }?>
</select>
</td> </tr>
<tr>
<td>Weight:</td>
<td><input type="text" name="weight" /></td>

</tr>
<tr>
<td colspan="2"><input type="submit" value="GetRate" /></td>
</tr>
</table>
</form>


getRateByTypeRespones.php代码:

<?php
// URL to call
define('REQUIRERATE','http://www.sendfromchina.com/shipfee/out_rates');
$countryName = @$_POST['countryName'];
$weight = @$_POST['weight'];

// Construct the GetShippingRate call
$profile = '?country='.$countryName.'&weight='.$weight;
//Construct the GetShippingRate By mode
$profile .= '&mode='.$_POST['mode'];
require_once 'XMLGetRateByMode.php';
$doc = new DOMDocument();
$doc->load( REQUIRERATE .$profile);
// Load the call and capture the document returned by sendfromchina.com API
$dataset = $doc->getElementsByTagName( "rate" );
// Check to see if the response was loaded, else print an error
if(@$dataset->item(0)->nodeValue){
?>
<hr color="#6794B8" size="2" >
<table cellspacing="1" cellpadding="0" width="85%">
<tr align="center" bgcolor="#C3D5FD">
<th width="30%">Mail Service</th>
<th width="25%">Total Fee(&#165;CNY)</th>
<th width="25%">Delivery time(Workday)</th>
<th width="10%">Tracking</th>
<th width="10%">By volume</th>
</tr>
<?php
// For each Shippinh Rate Item node, and display all of them
foreach( $dataset as $row ){
$totalfees = $row->getElementsByTagName( "totalfee" );
$totalfee = $totalfees->item(0)->nodeValue;
$deliverytimes = $row->getElementsByTagName( "deliverytime" );
$deliverytime = $deliverytimes->item(0)->nodeValue;
$iftrackings = $row->getElementsByTagName( "iftracking" );
$iftracking = $iftrackings->item(0)->nodeValue;
$classtypes = $row->getElementsByTagName( "classtype" );
$classtype = $classtypes->item(0)->nodeValue;
$byvolumes = $row->getElementsByTagName( "byvolume" );
$byvolume = $byvolumes->item(0)->nodeValue;
$shiptypes = $row->getElementsByTagName( "shiptypename" );
$shiptypename = $shiptypes->item(0)->nodeValue;
?>
<tr align="center" bgcolor="#ffffff">
<td width="30%"><?php echo $shiptypename ?></td>
<td width="25%"><?php echo $totalfee ?></td>
<td width="25%"><?php echo $deliverytime ?></td>
<td width="10%"><?php echo $iftracking ?></td>
<td width="10%"><?php echo $byvolume ?></td>
</tr>
<?php
}
?>
</table>
<?php
}else{
//display error message
$errors = $doc->getElementsByTagName( "errors" );
$error = $errors->item(0)->nodeValue;
echo $error;

}

?>

 

查询国际包裹跟踪信息

此查询需要提交ISHIP提供的订单ID或者ISHIP提供的跟踪号码。在提交查询前,我们应生成一个表单来输入订单ID或者跟踪号码,再提交查询。

代码:
<h2>Get Track Information </h2>
<form action="getTrackInfoRespone.php" method="post">
<table cellpadding="0">
<tr>
<td>Track &amp; Confirm :</td>
<td><input type ="text" name= "trackingNumber" value="" /></td>
<td><input type="submit" value= "Track" name ="Track"/></td>
</tr>
</table>
</form>


getTrackInfoRespone.php

<?php
header("Content-type: text/html; charset=utf-8");
define('REQUIRETRACKING','http://www.sendfromchina.com/tracking/to_xml');
$trackingNumber = @$_POST['trackingNumber'];
if($trackingNumber == ''){
echo "Please fill the Track Number.";
return;
}
?>
<h2>Get Track Information </h2>

<form action="./getTrackInfoRespone.php" method="post">
<table cellpadding="0">
<tr>
<td>Track &amp; Confirm :</td>
<td><input type ="text" name= "trackingNumber" value="" /></td>
<td><input type="submit" vale= "Track" name ="Track"/></td>
</tr>
</table>
</form>
<hr color="#6794B8" size="2" >
<?php
$profile = '?number='.$trackingNumber;
$doc = new DOMDocument();
$doc->load( REQUIRETRACKING .$profile);
// Load the call and capture the document returned by sendfromchina.com API
$order = $doc->getElementsByTagName( "Order" );
// Check to see if the response was loaded, else print an error
if(@$order->item(0)->nodeValue){
$order_id = $doc->getElementsByTagName( "order_id" );
$orderId = $order_id->item(0)->nodeValue;
$orderWeight = $doc->getElementsByTagName( "weight" );
$weight = $orderWeight->item(0)->nodeValue;
$orderType = $doc->getElementsByTagName( "type" );
$type = $orderType->item(0)->nodeValue;
$orderDestination = $doc->getElementsByTagName( "destination" );
$destination = $orderDestination->item(0)->nodeValue;

?>


<h3>Order Information </h3>
<table cellspacing="1" cellpadding="1" width="85%">
<tr >
<td>Order Id:</td><td><?php echo $orderId ?> </td> <td>&nbsp;&nbsp;&nbsp;</td><td>Weight:</td><td><?php echo $weight ?> </td>
</tr>
<tr >
<td>Type:</td><td><?php echo $type ?> </td> <td>&nbsp;&nbsp;&nbsp;</td><td>Destination:</td><td><?php echo $destination ?> </td>
</tr>
</table>
<?php

$trackingHistory = $doc->getElementsByTagName( "Track" );
if(@$trackingHistory->item(0)->nodeValue){
?>

<h3>Shipment Travel History </h3>
<table cellspacing="1" cellpadding="0" width="85%">
<tr align="center" bgcolor="#C3D5FD">
<td>Activity</td>
<td>TrackDate </td>
<td>Location</td>
</tr>
<?php
foreach( $trackingHistory as $row ){
$track_date = $row->getElementsByTagName( "track_date" );
$trackDate = $track_date->item(0)->nodeValue;
$locations = $row->getElementsByTagName( "locations" );
$location = $locations->item(0)->nodeValue;
$activitys = $row->getElementsByTagName( "activity" );
$activity = $activitys->item(0)->nodeValue;


?>



<tr align="center" >
<td><?php echo $activity ?></td>
<td><?php echo $trackDate ?></td>
<td><?php echo $location ?></td>
</tr>
<?php
}
?>
</table>
<?php
}
}else{
$errors = $doc->getElementsByTagName( "message" );
$error = $errors->item(0)->nodeValue;
echo $error;


}
?>