使用 php对接 Aramexs 物流
一、打开 Aramexs 官网 https://www.aramex.com/sa/en/aramex-home,并注册账号
二、注册后,设置一下自己是发货地址(地址记得用国际的国家编码哦)
三、熟悉开发者文档,由于业务需要,只对接了发货(createShipments),其他功能未对接 https://www.aramex.com/docs/default-source/resourses/resourcesdata/shipping-services-api-manual.pdf
四、通过github获取一下大佬代码 https://github.com/hakanersu/amaran-laravel 在大佬的基础上,调整了一下 https://github.com/yanggcn/php-aramex
五、上代码
public function createShipment(){
$result = Aramex::createShipment([
'shipper' => [
'name' => 'SA',
'email' => 'email@users.companies',
'phone' => '+123456789982',
'cell_phone' =>'+123456789982',
'country_code' => 'SA',
'city' => 'SA',
'zip_code' => '',
'line1' => '',
],
'consignee' => [
'name' => 'Steve',
'email' => 'email@users.companies',
'phone' => '+123456789982',
'cell_phone' => '+321654987789',
'country_code' => 'AE',
'city' => 'Abadilah',
'zip_code' => '',
'line1' => 'Line1 Details',
'line2' => 'Line2 Details',
'line3' => 'Line3 Details',
],
'shipping_date_time' => time() + 50000,
'due_date' => time() + 60000,
'comments' => 'No Comment',
'pickup_location' => 'Reception',
'customs_value_amount' => 100,
'weight' => 1,
'number_of_pieces' => 1,
'description' => '商品描述',
]);
if ($result->HasErrors) {
return $result->Notifications->Notification;
}elseif ($result->errors){
return $result->Notifications->Notification;
}else{
$results = $result->Shipments->ProcessedShipment;
$label_url = $results->ShipmentLabel->LabelURL;
$shipment_no = $results->ID;
}
return $results ;
}
六,获取国家城市信息
public function getCountries($country_code='',$is_city=0){
if($is_city){
$list = Aramex::fetchCities($country_code);
}else{
$list = Aramex::fetchCountries($country_code);
}
return $list;
}
public static function fetchCities($code, $nameStartWith = null)
{
$soapClient = AramexHelper::getSoapClient(AramexHelper::LOCATION);
$aramex = new Core;
$aramex->initializeFetchCities($code, $nameStartWith);
$call = $soapClient->FetchCities($aramex->getParam());
$ret = new \stdClass;
if ($call->HasErrors) {
$ret->error = 1;
$ret->errors = $call->Notifications;
}
else{
$ret = $call;
}
return $ret;
}
七,注意事项 1、国家城市数据比较多,如果存数据库里,查询的时候需要优化一下,否则你会卡死的 2、发货时填写的国家区号,需要注意下,用的international code 哦!
|