天天看點

PHP簡單實作快遞接口api

PHP簡單實作快遞接口api

首先需要進入阿裡雲

搜尋快遞接口
PHP簡單實作快遞接口api

我們就找到第一個就可以有免費的測試次數

PHP簡單實作快遞接口api

點開之後選擇購買免費次數

PHP簡單實作快遞接口api

點選控制台進入雲市場會看到購買的快遞接口資訊

PHP簡單實作快遞接口api
PHP簡單實作快遞接口api

這裡邊的内容等會會用到

接下來寫html頁面,我這裡就寫個簡單的
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<form action="phoneapi.php" method="get">
		
		快遞單号:<input type="text" name=\'no\'><br>
		快遞公司: <select name="type" id="">
				<option value="HTKY">百世快遞</option>
		//這裡在雲市場裡邊可以看到每個快遞有對應的預設值我這裡就拿百世快遞舉例子
		</select>	<br>
		<input type="submit"  value="查詢">
	</form>
</body>
</html>
           

背景PHP代碼

<?php
 $host = "https://wuliu.market.alicloudapi.com";//api通路連結
    $path = "/kdi";//API通路字尾
    $method = "GET";
    $no = $_GET[\'no\'];//快遞單号
    $type = $_GET[\'type\'];//選擇快遞公司 也可以不選擇但是準确率會降低一點
    $appcode = "41dc9e497e0b4d5a8xxxxxxx";//替換成自己的阿裡雲appcode
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "no=$no&type=$type";  //參數寫在這裡
    $bodys = "";
    $url = $host . $path . "?" . $querys;//url拼接

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
     $res = json_decode(curl_exec($curl));//這裡為傳回資訊
    var_dump($res);die;

           

示範一遍

PHP簡單實作快遞接口api

點選查詢之後

PHP簡單實作快遞接口api

這裡邊傳回的就是快遞的目前資訊以及狀态

當然也可以通過ajax異步請求使用者體驗效果會更好