PHP SOAP 购物网站集成示例代码


一般的网上购物网站都会有运输计算这一环节,如果你想使用我们的国际包裹资费计算集成到你们的网站,主要是有两个重要的部分,商品显示页面运费计算与下订单过程中的运输费用。

 

商品显示页面运费计算

在产品显示页面,通常会根据产品的重量、用户选择产品的个数与目的地显示平邮与快递的价格,供用户参考。在集成过程中,可能调用我们的getRates获取ISHIP的所有可行的运费计算规则,然后搞照ISHIP的分类(China Postal Service,China Registered Postal,China Express Mail)取得第一个也就是价格最低的此类型的运输方式,供用户选择。

代码:
<?php
$countryName = @$_POST['countryName'];
$quantity = @$_POST['quantity'];
if($quantity == null){
$quantity = 1;
}
if($countryName == null){
$countryName = "Canada";
}

$preItemFee = 18;//设定产品价格
$preWeight = 0.125;//设定产品重量

$client=new SoapClient('http://www.sendfromchina.com/shipfee/web_service?wsdl');
try{

//调用getCountries,取得国家列表
$result=$client->getCountries();


//require getRates
$respones = $client->getRates($preWeight * $quantity,$countryName);


}catch(SoapFault$e){
print"Sorry an error was caught executing your request:{$e->getMessage()}";
}
?>

 

<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="./SOAPCalculate.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">Customer Address(Country):</td>
<td colspan="2"><label>
<select name="countryName" id="countryName" onchange="return shipincart_submit();">
<?php

//遍历国家对象集,并生成国家选项--iteration the country object,and print every country
foreach( $result->countries as $country ){

?>

<option value='<?php echo $country->enName ?>'><?php echo $country->enName ?></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
if($respones){//if there have any result



if(count($respones->rates,1)==1){//if return result have only one rate
switch ($respones->rates->classtypecode){
case 'CPS':
$name = 'Standard';
break;
case 'CRP':
$name = 'Registered';
break;
case 'CEM':

$name = 'Expedited';
break;
}

?>


<tr>
<td><?php echo $name; ?></td>
<td><?php echo $respones->rates->totalfee ?> CNY</td>
<td><?php echo $respones->rates->totalfee + $preItemFee*$quantity ?> CNY</td>
</tr>

<?php
}else{
$Rates = NULL;
foreach( $respones->rates as $row ){



switch ($row->classtypecode){
case 'CPS':

$name = 'Standard';
break;
case 'CRP':

$name = 'Registered';
break;
case 'CEM':

$name = 'Expedited';
break;
}

if(@$Rates[$row->classtypecode] != 1){//是否已经存在此类型的SHIPPINGRATES记录,如果不存在就显示,已存在就是已经显示了价格比它低的RATES
$totalfee = $row->totalfee;
$shiptypecode = $row->shiptypecode;
$Rates[$row->classtypecode] = 1;
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $totalfee ?> CNY</td>
<td><?php echo $totalfee + $preItemFee*$quantity ?> CNY</td>
</tr>
<?php


}

}

}


}
?>
</tbody>
</table> </td>
</tr>
</table>
</form>

 

下订单过程中的运输费用

同理,在用户下订单过程中,需要提供运输方式会用户选择,并把运输费用写入到订单中.在此过程中我们可以使用getRates获取ISHIP的所有可行的运费计算规则,供用户选择(其中要提供用户Shipping地址的国家,产品的重量);用户选择后提交ISHIP的运输方式代号(可从getRates中提取),再使用我们提供的getRatesByMode取得在此订单重量与运输目的地上使用此运输方式的详细信息。以下是模拟的购物车样例代码:

cart.php
<?php
session_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&#165;<?php echo $preItemFee; ?></td>
<td>CNY&#165;<?php echo $preItemFee*$quantity; ?> </td>
</tr>
</table>
<div style="float:right">Subtotal:CNY&#165;<?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

<?php
session_start();
$client=new SoapClient('http://www.sendfromchina.com/shipfee/web_service?wsdl');
$_SESSION['defaultAddress']['string'] = '<strong>Test User</strong><br />
Nanshang,Shengzheng Guangdong<br />
Shengzheng,AR,518000<br />
Switzerland<br />
Phone:888166222';
$_SESSION['defaultAddress']['country'] = 'Canada';
try{

//调用getCountries,取得国家列表
$result=$client->getCountries();

//require getRates
//$respones = $client->getRates($preWeight * $quantity,$countryName);



}catch(SoapFault$e){
print"Sorry an error was caught executing your request:{$e->getMessage()}";
}
?>
<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

//遍历国家对象集,并生成国家选项--iteration the country object,and print all countrys
foreach( $result->countries as $country ){

?>
<option value='<?php echo $country->enName ?>'><?php echo $country->enName ?></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();
$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;

$client=new SoapClient('http://www.sendfromchina.com/shipfee/web_service?wsdl');
try{
//调用getShipTypes
$result=$client-> getClassTypes ();
$respones = $client->getRates($preWeight * $quantity,$countryName);

}catch(SoapFault $e){
print "Sorry an error was caught executing your request:{$e->getMessage()}";
}

?>

 

 

<h4> shipping address:</h4>
<div style="margin-left:15px">

<p><?php echo $address; ?>
</p>
</p>
<p>&nbsp; </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
if($respones){//if there have any result



if(count($respones->rates,1)==1){//if return result have only one rate
switch ($respones->rates->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;
}

?>

<ul>
<input name="shipping" value="<?php echo $respones->rates->shiptypecode ?> " id="shipping" rule="shipping" type="radio" checked="checked">
<label for="ship-chinaups-chinaups"> <strong><?php echo $name ?></strong> .............................................................. <strong> CNY&#165;<?php echo $respones->rates->totalfee ?> </strong></label>
</ul>
<ul>
Delivery Estimates: <?php echo $respones->rates->deliverytime ?> days to major destinations.
</ul>
<?php
}else{

foreach( $respones->rates as $row ){



switch ($row->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){
$totalfee = $row->totalfee;
$shiptypecode = $row->shiptypecode;
$deliverytime = $row->deliverytime;
$shiptypecode = $row->shiptypecode;
$Rates[$key] = ture;
?>

 

<ul>
<input name="shipping" value="<?php echo $shiptypecode ?>" type="radio" checked="checked">
<label for="ship-ems-ems"> <strong><?php echo $name ?></strong> ............................................................... <strong> CNY&#165;<?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

<?php
session_start();
$method = $_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;

$client=new SoapClient('http://www.sendfromchina.com/shipfee/web_service?wsdl');
try{
//调用getShipTypes
$result=$client-> getClassTypes ();
$respones = $client->getRateByMode($preWeight * $quantity,$countryName,$method);

}catch(SoapFault $e){
print "Sorry an error was caught executing your request:{$e->getMessage()}";
}
?>

<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&#165;<?php echo $preItemFee; ?></td>
<td>CNY&#165;<?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>
<tr>
<td><?php echo $respones->shiptypename; ?></td>
<td><?php echo $respones->shiptypecode;?></td>
<td><?php echo $respones->deliverytime;?></td>
<td><?php echo $respones->totalfee; ?></td>

</tr>
</table>

下载示例代码