PHP HTTP 购物网站集成示例代码
一般的网上购物网站都会有运输计算这一环节,如果你想使用我们的国际包裹资费计算集成到你们的网站,主要是有两个重要的部分,商品显示页面运费计算与下订单过程中的运输费用。
商品显示页面运费计算
在产品显示页面,通常会根据产品的重量、用户选择产品的个数与目的地显示平邮与快递的价格,供用户参考。在集成过程中,可能调用我们的getRates获取ISHIP的所有可行的运费计算规则,然后搞照ISHIP的分类(China Postal Service,China Registered Postal,China Express Mail)取得第一个也就是价格最低的此类型的运输方式,供用户选择。
代码:define('REQUIRECOUNTRYURL','http://www.sendfromchina.com/shipfee/country_list');//http require URL for iship country
define('REQUIRERATE','http://www.sendfromchina.com/shipfee/out_rates');//http require URL for iship shipping Rates
$countryName = @$_POST['countryName'];
$quantity = @$_POST['quantity'];
if($quantity == null){
$quantity = 1;
}
if($countryName == null){
$countryName = "Canada";
}
$preItemFee = 18;
$preWeight = 0.125;
$profile = '?country='.$countryName.'&weight='.$preWeight * $quantity ;
?>
<script language="javascript">
window.onload=function(){
document.forms['form1'].quantity.value= '<?php echo $quantity ?>';
document.forms['form1'].countryName.value= '<?php echo $countryName ?>';
}
function shipincart_submit(country){
document.form1.submit();
}
</script>
<h2>Estimate Shipping Costs</h2>
<form name="form1" action="./calculate.php" method="post">
<table cellspacing="0" cellpadding="0">
<tr>
<td width="141" height="35">Quantity:
<select name="quantity" id="quantity" onchange="return shipincart_submit();">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</td>
<td width="169">Weight: <strong><?php echo $preWeight * $quantity ;?></strong> kg</td>
<td width="180">Item cost: <strong><?php echo $preItemFee*$quantity ?> CNY</strong></td>
</tr>
<tr>
<td height="35">Shipping to(Country):</td>
<td colspan="2"><label>
<select name="countryName" id="countryName" onchange="return shipincart_submit();">
<?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>
</label></td>
</tr>
<tr>
<td colspan="3">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<th width="210" align="left">Available Shipping Methods</th>
<th align="left">Shipping Cost</th>
<th align="left">Total Cost</th>
</tr>
<?php
$doc1 = new DOMDocument();
$doc1->load( REQUIRERATE .$profile);
// Load the call and capture the document returned by sendfromchina.com API
$dataset = $doc1->getElementsByTagName( "rate" );
// Check to see if the response was loaded, else print an error
if(@$dataset->item(0)->nodeValue){
foreach( $dataset as $row ){
$classtypes = $row->getElementsByTagName( "classtype" );
$classtype = $classtypes->item(0)->nodeValue;
switch ($classtype){
case 'China Postal Service':
$key = 'CPS';
$name = 'Standard';
break;
case 'China Registered Postal':
$key = 'CRP';
$name = 'Registered';
break;
case 'China Express Mail':
$key = 'CEM';
$name = 'Expedited';
break;
}
if($Rates[$key] != ture){
$totalfees = $row->getElementsByTagName( "totalfee" );
$totalfee = $totalfees->item(0)->nodeValue;
$iftrackings = $row->getElementsByTagName( "iftracking" );
$byvolumes = $row->getElementsByTagName( "byvolume" );
$shiptypes = $row->getElementsByTagName( "shiptypename" );
$Rates[$key] = ture;
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $totalfee ?> CNY</td>
<td><?php echo $totalfee + $preItemFee*$quantity ?> CNY</td>
</tr>
<?php
}
}
?>
<?php
//var_dump($Rates);
}
?>
</tbody>
</table> </td>
</tr>
</table>
</form>
下订单过程中的运输费用
同理,在用户下订单过程中,需要提供运输方式会用户选择,并把运输费用写入到订单中.在此过程中我们可以使用getRates获取ISHIP的所有可行的运费计算规则,供用户选择(其中要提供用户Shipping地址的国家,产品的重量);用户选择后提交ISHIP的运输方式代号(可从getRates中提取),再使用我们提供的getRatesByMode取得在此订单重量与运输目的地上使用此运输方式的详细信息。以下是模拟的购物车样例代码:
cart.phpsession_start();
$quantity = @$_GET['quantity'];
if($quantity == null || $quantity == ''){
$quantity = 1;
}
$preItemFee = 18;//pre item price
$preWeight = 0.125;//pre item weight
?>
<script language="javascript">
window.onload = function(){
document.getElementById('cart_quantity').value = '<?php echo $quantity ?>';
}
function updateQuantity(){
var value = document.getElementById('cart_quantity').value;
document.forms['cart'].action = "./cart.php?quantity="+value;
document.forms['cart'].submit();
}
</script>
<form name="cart" action="./shippingaddress.php" method="post" id="cart">
<table cellpadding="1" border="1" width="100%">
<tr>
<td><strong>Item</strong> </td>
<td><strong>Product Name</strong> </td>
<td><strong>Quantity</strong> </td>
<td><strong>Pre Price</strong> </td>
<td><strong>Price</strong></td>
</tr>
<tr>
<td><img src="./img/item1.jpg" width="85" border="0" height="85"></a> </td>
<td>Super Quad Band Cellphone Plays Movies</td>
<td> <input name="cart_quantity" id="cart_quantity" value="1" size="4" onkeyup="value=value.replace(/[^\d]/g,'');"
type="text"><a href="#" onclick="updateQuantity();">Update</a> </td>
<td>CNY¥<?php echo $preItemFee; ?></td>
<td>CNY¥<?php echo $preItemFee*$quantity; ?> </td>
</tr>
</table>
<div style="float:right">Subtotal:CNY¥<?php echo $preItemFee*$quantity; ?></div>
<div style="clear: both "></div>
<div style="float:right"><input type="button" value="Checkout" onclick = "this.form.submit();" /></div>
</form>
<ul>
shippingaddress.php
session_start();
define('REQUIRECOUNTRYURL','http://www.sendfromchina.com/shipfee/country_list');
$_SESSION['defaultAddress']['string'] = '<strong>Test User</strong><br />
Nanshang,Shengzheng Guangdong<br />
Shengzheng,AR,518000<br />
Switzerland<br />
Phone:888166222';
$_SESSION['defaultAddress']['country'] = 'Canada';
?>
<form action="./checkout.php" name="" method="post">
<table cellpadding="1" border="1" width="90%">
<tr>
<td>
<input name="shippingType" type="radio" value="1" checked="checked" onclick="document.getElementById('newAddress').style.display='none'"/>
</td>
<td><?php echo $_SESSION['defaultAddress']['string']; ?>
<input type="hidden" name="countryName" value="<?php echo $_SESSION['defaultAddress']['country'] ?>" />
</td>
</tr>
<tr>
<td>
<input name="shippingType" type="radio" value="2" onclick="document.getElementById('newAddress').style.display='block'"/>
</td>
<td>New Shipping Address</td>
</tr>
</table>
<table cellpadding="1" border="1" width="90%" id="newAddress" style="display:none">
<tr>
<td>First name:*</td>
<td colspan="3">
<input name="firstname" value="" maxlength="32" id="firstname" chkname="First name" chkrule="nnull/min2" type="text"> </td>
</tr>
<tr>
<td>Last name:*</td>
<td>
<input name="lastname" value="" maxlength="32" id="lastname" chkname="Last name" chkrule="nnull/min2" type="text"> </td>
</tr>
<tr>
<td>Address Line1:*</td>
<td colspan="3">
<input name="street_address" value="" maxlength="64" id="street-address" chkname="Address Line1" chkrule="nnull/min5" type="text"> </td>
</tr>
<tr>
<td>Address Line2:</td>
<td colspan="3">
<input name="suburb" maxlength="64" id="suburb" type="text"> </td>
</tr>
<tr>
<td>City:*</td>
<td colspan="3"><input name="city" value="" maxlength="32" id="city" chkname="City" chkrule="nnull/min3" type="text"> </td>
</tr>
<tr>
<td>Country / Region:*</td>
<td colspan="3"><select name="countryName" id="countryName" onchange="return shipincart_submit();">
<?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 id="zone_id_tr">
<td>State/Province/Region:*</td>
<td colspan="3"><input name="zone" id="country" /></td>
</tr>
<tr>
<td>ZIP/Postal Code:*</td>
<td colspan="3"><input name="postcode" value="" maxlength="10" id="postcode" chkname="ZIP/Postal Code" chkrule="nnull/min4" type="text"> </td>
</tr>
<tr>
<td>Phone Number:*</td>
<td colspan="3"><input name="phone" value="" maxlength="45" id="phone" chkname="Phone Number" chkrule="nnull/min4/tel" type="text"></td>
</tr>
</table>
<div align="center">
<input type="submit" name="submit" value="submit" onclick="this.form.submit();"/>
</div>
</form>
checkout.php
<?php
session_start();
define('REQUIRERATE','http://www.sendfromchina.com/shipfee/out_rates');
$shippingType = $_POST['shippingType'];
if($shippingType == 1){//use default shipping address
$address = $_SESSION['defaultAddress']['string'];
}elseif($shippingType == 2){
$firstname = @$_POST['firstname'];
$lastname = @$_POST['lastname'];
$street_address = @$_POST['street_address'];
$suburb = @$_POST['suburb'];
$zone = @$_POST['zone'];
$postcode = @$_POST['postcode'];
$countryName = @$_POST['countryName'];
$phone = @$_POST['phone'];
$city = @$_POST['city'];
$address = $firstname." ".$lastname."</br>".
$street_address.' '.$suburb."</br>".
$city.','.$postcode."</br>".
$countryName."</br>".
"Phone:".$phone;
}else{
Header("Location: ./shippingaddress.php");
break;
}
$countryName = @$_POST['countryName'];
$quantity = @$_POST['quantity'];
if($quantity == null){
$quantity = 1;
}
if($countryName == null){
$countryName = "Canada";
}
$_SESSION['order']['shippingAddress'] = $address;
$_SESSION['order']['countryName'] = $countryName;
$_SESSION['order']['quantity'] = $quantity;
$preItemFee = 18;
$preWeight = 0.125;
$profile = '?country='.$countryName.'&weight='.$preWeight*$quantity;
?>
<h4> shipping address:</h4>
<div style="margin-left:15px">
<p><?php echo $address; ?>
</p>
</p>
<p> </p>
</div>
<div style="width:95%; height:auto">
<form action="./order.php" method="post">
<ul>
Please select the preferred shipping method to use on this order.
</ul>
<?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){
$Rates =NULL;
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;
$classtypecodes = $row->getElementsByTagName( "classtypecode" );
$classtypecode = $classtypecodes->item(0)->nodeValue;
$shiptypecodes = $row->getElementsByTagName( "shiptypecode" );
$shiptypecode = $shiptypecodes->item(0)->nodeValue;
switch ($classtypecode){
case 'CPS':
$name = 'Standard';
break;
case 'CRP':
$name = 'Registered';
break;
case 'CRE':
$name = 'Expedited';
break;
}
if(@$Rates[$classtypecode] != 1){
$Rates[$classtypecode] =1;
?>
<ul>
<input name="shipping" value="<?php echo $shiptypecode ?> " id="shipping" rule="shipping" type="radio" checked="checked">
<label for="ship-chinaups-chinaups"> <strong><?php echo $name ?></strong> .............................................................. <strong> CNY¥<?php echo $totalfee ?> </strong></label>
</ul>
<ul>
Delivery Estimates: <?php echo $deliverytime ?> days to major destinations.
</ul>
<?php
}
}
}
?>
<input type="submit" value="Submit" />
</form>
</div>
order.php
session_start();
define('REQUIRERATE','http://www.sendfromchina.com/shipfee/out_rates');
$method = trim($_POST['shipping']);
if($method == null){
Header("Location: ./checkout.php");
break;
}
$quantity = $_SESSION['order']['quantity'];
$address = $_SESSION['order']['shippingAddress'];
$countryName = $_SESSION['order']['countryName'] ;
$preItemFee = 18;
$preWeight = 0.125;
$profile = '?country='.$countryName.'&weight='.$preWeight*$quantity;
//Construct the GetShippingRate By mode
$profile .= '&mode='.$method;
?>
<h2>Order Information</h2>
<h4>Item</h4>
<table cellpadding="1" border="1" width="100%">
<tr>
<td><strong>Item</strong> </td>
<td><strong>Product Name</strong> </td>
<td><strong>Quantity</strong> </td>
<td><strong>Pre Price</strong> </td>
<td><strong>Price</strong></td>
</tr>
<tr>
<td><img src="./img/item1.jpg" width="85" border="0" height="85"></a> </td>
<td>Super Quad Band Cellphone Plays Movies / + Camera </td>
<td> <?php echo $quantity; ?> </td>
<td>CNY¥<?php echo $preItemFee; ?></td>
<td>CNY¥<?php echo $preItemFee*$quantity; ?> </td>
</tr>
</table>
<br />
<h4> shipping address:</h4>
<div style="margin-left:15px">
<?php echo $address; ?>
</div>
<h4> shipping method:</h4>
<table cellpadding="1" border="1" width="100%">
<tr>
<td><strong>Shipping Service</strong> </td>
<td><strong>Iship Shipping Type Code</strong> </td>
<td><strong>Shipping Time</strong> </td>
<td><strong>Shipping Price</strong> </td>
</tr>
<?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){
$totalfees = $doc->getElementsByTagName( "totalfee" );
$totalfee = $totalfees->item(0)->nodeValue;
$deliverytimes = $doc->getElementsByTagName( "deliverytime" );
$deliverytime = $deliverytimes->item(0)->nodeValue;
$iftrackings = $doc->getElementsByTagName( "iftracking" );
$iftracking = $iftrackings->item(0)->nodeValue;
$classtypes = $doc->getElementsByTagName( "classtype" );
$classtype = $classtypes->item(0)->nodeValue;
$byvolumes = $doc->getElementsByTagName( "byvolume" );
$byvolume = $byvolumes->item(0)->nodeValue;
$shiptypes = $doc->getElementsByTagName( "shiptypename" );
$shiptypename = $shiptypes->item(0)->nodeValue;
$shiptypecodes = $doc->getElementsByTagName( "shiptypecode" );
$shiptypecode = $shiptypecodes->item(0)->nodeValue;
?>
<tr>
<td><?php echo $shiptypename; ?></td>
<td><?php echo $shiptypecode;?></td>
<td><?php echo $deliverytime;?></td>
<td><?php echo $totalfee; ?></td>
</tr>
<?php
}
?>
</table>
-
效率很高,能够做到下午准时收货,第二天早上准时发货评价人:B**1
-
希望你们网点多一些,我天天发小包到你们公司,有点麻烦!评价人:K**8
-
刚用三态,发了快10票的货,感觉总体服务还不错,服务态度好,解决问题很快。评价人:C**2
-
还好你们晚上也收件,这样就有比较宽敞的时间打包.如果时间再延后一些就更好了,谢谢.评价人:Z**3
-
价格查询功能很好,价格透明一目了然,特方便。评价人:Z**3
-
我很喜欢用你们的系统,能够为大客户着想,有很多实用的批量管理功能。评价人:G**9
-
拿以前使用的几家货代公司比较,我发现三态的网站还是很好用的,跟踪查询都比较方便,内容也很丰富。评价人:B**5
-
三态网站的的价格查询工具很方便,很实用,只要输入目的地,就能搜索到所有相关的投寄方式供我选择,节省了 ...评价人:O**5










