共计 2676 个字符,预计需要花费 7 分钟才能阅读完成。
一、接口申请
通过聚合 https://www.juhe.cn/docs/api/id/43 自助申请开明接口权限。
二、通过 PHP 发动快递查问
class JuheExpress
{
private $apiKey = '**************'; // 在聚合申请到的快递查问接口申请 Key
private $apiHost = 'https://v.juhe.cn/exp';
/**
* 获取反对的快递公司及编号
*/
public function getCompanys()
{
// 组装申请残缺 URL
$requestApiUrl = $this->apiHost . '/com?key=' . $this->apiKey;
// 发动网络申请聚合
$requestResponseContent = $this->juheHttpRequest($requestApiUrl);
// 解析后果
$requestResult = json_decode($requestResponseContent, true);
if (!$requestResult) {
// 申请异样,可能网络异样等起因,依据本身逻辑批改
echo "申请异样";
return false;
} else {$requestStatusCode = $requestResult['error_code'];
if ($requestStatusCode === 0) {
// 申请胜利,依据本身逻辑批改
$companys = $requestResult['result'];
return $companys;
} else {
// 申请失败,依据本身逻辑批改
echo $requestResult['error_code'] . ':' . $requestResult['reason'];
return false;
}
}
}
/**
* 依据快递公司编号和快递单号查问快递状态
*/
public function getResult()
{
$apiUrl = $this->apiHost . '/index';
$requestParams = [
'key' => $this->apiKey,
'com' => 'yd', // 须要查问的快递公司编号
'no' => '4312453259458', // 须要查问的快递单号
'senderPhone' => '','receiverPhone'=>''
];
// 发动网络申请聚合
$requestResponseContent = $this->juheHttpRequest($apiUrl, $requestParams, 1);
// 解析后果
$requestResult = json_decode($requestResponseContent, true);
if (!$requestResult) {
// 申请异样,可能网络异样等起因,依据本身逻辑批改
echo "申请异样";
return false;
} else {$requestStatusCode = $requestResult['error_code'];
if ($requestStatusCode === 0) {
// 申请胜利,依据本身逻辑批改
$companys = $requestResult['result'];
return $companys;
} else {
// 申请失败,依据本身逻辑批改
echo $requestResult['error_code'] . ':' . $requestResult['reason'];
return false;
}
}
}
/**
* 发动网络申请函数
* @param $url 申请的 URL
* @param bool $params 申请的参数内容
* @param int $ispost 是否 POST 申请
* @return bool|string 返回内容
*/
private function juheHttpRequest($url, $params = false, $ispost = 0)
{// $httpInfo = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 12);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($ispost) {curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {if ($params) {if (is_array($params)) {curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params));
} else {curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
}
} else {curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
if ($response === false) {// echo "cURL Error:".curl_error($ch);
return false;
}
// $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
return $response;
}
}
/**
* 聚合罕用快递 API 发动申请 -PHP 代码
* 性能:反对大多数快递公司快递单号查问
*/
$juheExpress = new JuheExpress();
$companys = $juheExpress->getCompanys();
print_r($companys);
$results = $juheExpress->getResult();
print_r($results);
正文完