日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php开放平台,顺丰开放平台API PHP SDK demo

發布時間:2023/12/20 php 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php开放平台,顺丰开放平台API PHP SDK demo 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

順豐開放平臺Api PHP SDK demo

引用

對接前期工作注冊

提交接入申請

接口初始化配置,獲得 app_id 和app_secret

完成測試環境測試

進入生產使用

接入注意事項申請令牌 (/security/access_token/ 接口)

access_token 有效期為 1 小時,過期需使用 refresh_token 刷新令牌(/security/refresh_token/ 接口)

refresh_token 有效期為當天 24 點,過期需重新申請(/security/access_token/ 接口)

接口均為 https ,數據傳輸格式為 json

Demo

SDK Demo<?php

/**

* 順豐 api 接口使用

* Date: 2017/10/19

* Time: 9:36

* @author zlh

*/

class SfApi {

/**

* @var string

*/

protected $app_id;

/**

* @var string

*/

protected $app_key;

/**

* @var string

*/

public $access_token;

/**

* @var string

*/

public $refresh_token;

/**

* @var string

*/

private $uri;

/**

* SfApi constructor.

* @param string $app_id

* @param string $app_key

* @param bool $online

* @return void

*/

public function __construct ($app_id, $app_key, $online = false) {

$this->app_id = $app_id;

$this->app_key = $app_key;

$this->url = $online ? 'https://open-prod.sf-express.com/' : 'https://open-sbox.sf-express.com/';

}

/**

* 快速創建訂單

* @param array $req_body

* @return bool|string

*/

public function order ($req_body) {

$req_body = array(

'orderId' => $req_body['orderId'],

'expressType' => $req_body['expressType'],

'payMethod' => $req_body['payMethod'],

'custId' => $req_body['custId'],

'payArea' => isset($req_body['payArea']) ? $req_body['payArea'] : 'SFCM10008035754399',

//'remark' => $req_body['remark'],

'consigneeInfo' => array(

'company' => isset($req_body['consigneeInfo']['company']) ? $req_body['consigneeInfo']['company'] : '個人',

'contact' => $req_body['consigneeInfo']['contact'],

'tel' => $req_body['consigneeInfo']['tel'],

'province' => $req_body['consigneeInfo']['province'],

'city' => $req_body['consigneeInfo']['city'],

'county' => $req_body['consigneeInfo']['county'],

'address' => $req_body['consigneeInfo']['address'],

'mobile' => $req_body['consigneeInfo']['mobile'],

),

'cargoInfo' => array(

'cargo' => $req_body['cargoInfo']['cargo'],

),

'addedServices' => $req_body['addedServices'],

);

$res = $this->send('order', $req_body);

$res_tmp = json_decode($res, true);

if(empty($res_tmp['head']['code'])) {

return '系統錯誤!';

}elseif(substr($res_tmp['head']['code'], -3) != '200') {

return $res_tmp['head']['message'];

}else{

return true;

}

}

/**

* 訂單結果查詢

* @param string $order_id

* @return json

*/

public function orderQuery ($order_id) {

$req_body = array(

'orderId' => $order_id

);

return $this->send('order/query', $req_body);

}

/**

* 根據 運單號/訂單號 查詢物流路由

* @param array $req_body

* @return json

*/

public function routeQuery ($req_body) {

$req_body = array(

'trackingType' => $req_body['trackingType'],

'trackingNumber' => $req_body['trackingNumber'],

'methodType' => $req_body['methodType'],

);

return $this->send('route/query', $req_body);

}

/**

* 電子運單圖片下載

* @param string $orderId

* @param bool $isLogo

* @return json (image參數為BASE64編碼的字符串)

*/

public function wayBillImage ($orderId, $isLogo = false) {

$req_body = array(

'orderId' => $orderId,

'isLogo' => $isLogo ? 1 : 0

);

return $this->send('waybill/image', $req_body);

}

/**

* 獲取順豐授權

* @return json

*/

public function getToken () {

return $this->send('security/access_token');

}

/**

* 刷新授權令牌

* @param string $access_token

* @param string $refresh_token

* @return json

*/

public function refreshToken ($access_token, $refresh_token) {

$this->access_token = $access_token;

$this->refresh_token = $refresh_token;

return $this->send('security/refresh_token');

}

/**

* 忘記 access_token 查詢

* @return json

*/

public function queryToken () {

return $this->send('security/access_token/query');

}

/**

* 根據請求的接口創建相應的 Uri

* @param string $resource 要請求的resource(前后不帶'/')

* @return string

*/

protected function buildUri ($resource) {

$query = '/sf_appid/' . $this->app_id . '/sf_appkey/' . $this->app_key;

if ($resource == 'security/refresh_token') {

$query = "/access_token/$this->access_token/refresh_token/$this->refresh_token" . $query;

}elseif (substr($resource, 0, 8) != 'security') {

$query = '/access_token/' . $this->access_token . $query;

}

$uri = $this->url . $this->transType()[$resource]['type'] . '/v1.0/' . $resource . $query;

return $uri;

}

/**

* 向順豐接口發送請求

* SSL POST JSON

* @param string $resource 要請求的resource(前后不帶'/')

* @param array $body 請求報文(參數)

* @return json

*/

protected function send ($resource, $body = array()) {

$this->uri = $this->buildUri($resource);

$req_data = array(

'head' => array(

'transType' => $this->transType()[$resource]['transType'],

'transMessageId' => date('Ymd') . substr_replace(time(), mt_rand(10,99),0,2),

)

);

if(!empty($body)) {

$req_data['body'] = $body;

}

$header = array(

"Content-Type: application/json",

);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $this->uri);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 從證書中檢查SSL加密算法是否存在

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($req_data));

$response = curl_exec($ch);

curl_close($ch);

return $response;

}

/**

* 順豐開放平臺各接口對應的資源類型

* @return array

*/

private function transType () {

return array(

'order' => array('transType' => 200, 'type' => 'rest'),

'order/query' => array('transType' => 203, 'type' => 'rest'),

'filter' => array('transType' => 204, 'type' => 'rest'),

'route/query' => array('transType' => 501, 'type' => 'rest'),

'route/inc/query' => array('transType' => 504, 'type' => 'rest'),

'waybill/image' => array('transType' => 205, 'type' => 'rest'),

'product/basic/query' => array('transType' => 250, 'type' => 'rest'),

'product/additional/query' => array('transType' => 251, 'type' => 'rest'),

'security/access_token/query' => array('transType' => 300, 'type' => 'public'),

'security/access_token' => array('transType' => 301, 'type' => 'public'),

'security/refresh_token' => array('transType' => 302, 'type' => 'public'),

);

/**

快速下單 /order/ 200

訂單查詢 /order/query/ 203

訂單篩選 /filter/ 204

路由查詢 /route/query/ 501

路由增量查詢 /route/inc/query/ 504

電子運單圖片下載 /waybill/image/ 205

基礎服務查詢 /product/basic/query/ 250

附加服務查詢 /product/additional/query/ 251

申請訪問令牌 /security/access_token/ 301

查詢訪問令牌 /security/access_token/query/ 300

刷新訪問令牌 /security/refresh_token/ 302

*/

}

}

使用引入該類文件

并實例化,參數分別為 app_id, app_secret, 是否用于生產環節 (true|false),默認為 false。

檢查 access_token 并設置。

調用對應方法即可,詳細使用請參考方法注釋和順豐接口文檔。

例子// 實例化對象并傳入參數

$sf = new SfApi('your app_id', 'your app_secret', false);

// 獲取 access_token

$token_res = $sf->getToken();

var_dump($token_res);

// 為對象設置 access_token 屬性

$sf->access_token = json_decode($token_res, true)['body']['accessToken'];

// 下單

$req_body = array(

'orderId' => 'SF201710091507536283',

'expressType' => 1,

'payMethod' => 3,

'custId' => 'your custId',

'payArea' => '010EU',

'remark' => '備注',

'consigneeInfo' => array(

'company' => '個人',

'contact' => '測試對象',

'tel' => '17600000000',

'province' => '北京市',

'city' => '北京市',

'county' => '朝陽區',

'address' => '十里河',

'mobile' => '17600000000',

),

'cargoInfo' => array(

'cargo' => '產品名',

),

// 代收貨款需填,標準快遞則不需要如下參數

'addedServices' => array(

array(

'name' => 'COD',

'value' => '698',

),

array(

'name' => 'CUSTID',

'value' => 'your custId'

)

)

);

$order_res= $sf->order($req_body);

var_dump($order_res);

// 獲取運單圖片

$image_res = $sf->wayBillImage('SF201710091507536283');

$image_res = json_decode($image_res, true);

file_put_contents('./SF201710091507536283.jpg', base64_decode($image_res['body']['images'][0]));

echo '';

運單圖片

示例

圖中馬賽克為手打

運單圖片處理

處理圖片使用 GD 或 imagemagick 等處理圖片的庫。

一般來說需要隱藏客戶的收件信息,下附上圖片處理相關函數。/** 圖片局部打馬賽克

* @param String $source 原圖

* @param Stirng $dest 生成的圖片

* @param int $x1 起點橫坐標

* @param int $y1 起點縱坐標

* @param int $x2 終點橫坐標

* @param int $y2 終點縱坐標

* @param int $deep 深度,數字越大越模糊

* @param bool $fill 是否只填充白色

* @return boolean

*/

function imageMosaics($source, $dest, $x1, $y1, $x2, $y2, $deep, $fill = false){

// 判斷原圖是否存在

if(!file_exists($source)){

return false;

}

// 獲取原圖信息

list($owidth, $oheight, $otype) = getimagesize($source);

// 判斷區域是否超出圖片

if($x1>$owidth || $x1<0 || $x2>$owidth || $x2<0 || $y1>$oheight || $y1<0 || $y2>$oheight || $y2<0){

return false;

}

switch($otype){

case 1: $source_img = imagecreatefromgif($source); break;

case 2: $source_img = imagecreatefromjpeg($source); break;

case 3: $source_img = imagecreatefrompng($source); break;

default:

return false;

}

// 打馬賽克

if($fill === false) {

for($x=$x1; $x

for($y=$y1; $y

$color = imagecolorat($source_img, $x+round($deep/2), $y+round($deep/2));

imagefilledrectangle($source_img, $x, $y, $x+$deep, $y+$deep, $color);

}

}

}else{

$im = imagecreatetruecolor(200, 200);

$fill = imagecolorallocate($im, 255, 255, 255);

imagefilledrectangle($source_img, $x1, $y1, $x2, $y2, $fill);

}

// 生成圖片

switch($otype){

case 1: imagegif($source_img, $dest); break;

case 2: imagejpeg($source_img, $dest); break;

case 3: imagepng($source_img, $dest); break;

}

return is_file($dest)? true : false;

}// 使用

$source = $dest = './201710244108826825.jpg';

// 5,5 1125,100 區域填充白色(應對帶有順豐logo的打印紙)

$flag = imageMosaics($source, $dest, 5, 5, 1125, 100, 4, true);

// (120)215, 250 810,400 區域馬賽克

$flag = imageMosaics($source, $dest, 215, 250, 810, 400, 7);

// (110)210,1207 678,1314 區域馬賽克

$flag = imageMosaics($source, $dest, 210, 1207, 678, 1314, 7);

echo '';該函數為以前網絡上找到并改造的,未找到原作者。如有侵權請聯系我刪除。

處理后示例

圖中 logo 處和收件人處為函數生成的馬賽克、填充白色,其余區域為手打馬賽克。

其他

代碼寫的比較倉促,不過一般的與順豐對接也就這些基本功能,還有一些接口本文沒有用到的,可以參考上面 SDK 中對應函數的注釋和接口文檔。

注:轉載請注明出處。

本文由 root 創作,采用 知識共享署名4.0 國際許可協議進行許可

本站文章除注明轉載/出處外,均為本站原創或翻譯,轉載前請務必署名

最后編輯時間為: Sep 28, 2018 at 12:10 pm

總結

以上是生活随笔為你收集整理的php开放平台,顺丰开放平台API PHP SDK demo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。