208 lines
6.2 KiB
PHP
208 lines
6.2 KiB
PHP
<?php namespace App\Utils;
|
|
|
|
use App\Http\Models\ShipperModel;
|
|
|
|
class Http
|
|
{
|
|
|
|
//查询快递专用post
|
|
static function shipperPost($url, $datas) {
|
|
$temps = array();
|
|
foreach ($datas as $key => $value) {
|
|
$temps[] = sprintf('%s=%s', $key, $value);
|
|
}
|
|
$post_data = implode('&', $temps);
|
|
$url_info = parse_url($url);
|
|
$httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
|
|
$httpheader.= "Host:" . $url_info['host'] . "\r\n";
|
|
$httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
|
|
$httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
|
|
$httpheader.= "Connection:close\r\n\r\n";
|
|
$httpheader.= $post_data;
|
|
$fd = fsockopen($url_info['host'], 80);
|
|
fwrite($fd, $httpheader);
|
|
$gets = "";
|
|
$headerFlag = true;
|
|
while (!feof($fd)) {
|
|
if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
while (!feof($fd)) {
|
|
$gets.= fread($fd, 128);
|
|
}
|
|
fclose($fd);
|
|
return $gets;
|
|
}
|
|
|
|
/**
|
|
* 模拟POST
|
|
*
|
|
* @param $url
|
|
* @param $data
|
|
*
|
|
* @return mixed|string
|
|
*/
|
|
public static function post($url, $data)
|
|
{
|
|
if (!function_exists('curl_init'))
|
|
{
|
|
return '';
|
|
}
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
# curl_setopt( $ch, CURLOPT_HEADER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
$data = curl_exec($ch);
|
|
if (!$data)
|
|
{
|
|
error_log(curl_error($ch));
|
|
}
|
|
curl_close($ch);
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 模拟GET
|
|
*
|
|
* @param $url
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function get($url)
|
|
{
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
# curl_setopt($ch, CURLOPT_HEADER, 1);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
|
|
|
|
if (!curl_exec($ch))
|
|
{
|
|
error_log(curl_error($ch));
|
|
$data = '';
|
|
}
|
|
else
|
|
{
|
|
$data = curl_multi_getcontent($ch);
|
|
}
|
|
curl_close($ch);
|
|
|
|
return $data;
|
|
}
|
|
|
|
public static function orderTracking($shipper, $trackingnums, $trade_no=0){
|
|
|
|
$EBusinessID = '1256285';
|
|
$AppKey = '1a4dac6f-7666-4cf4-802e-c503aff437b8';
|
|
$ReqURL = 'http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx';
|
|
|
|
$trackingnums = array_map('trim', explode('|', $trackingnums));
|
|
$data = [];
|
|
//todo:一个订单有多个快递公司?
|
|
foreach($trackingnums as $trackingnum){
|
|
if(!$trackingnum){
|
|
continue;
|
|
}
|
|
$requestData= "{\"OrderCode\":\"".$trade_no."\",\"ShipperCode\":\"".$shipper."\",\"LogisticCode\":\"".$trackingnum."\"}";
|
|
$datas = array(
|
|
'EBusinessID' => $EBusinessID,
|
|
'RequestType' => '1002',
|
|
'RequestData' => urlencode($requestData) ,
|
|
'DataType' => '2',
|
|
);
|
|
$datas['DataSign'] = urlencode(base64_encode(md5($requestData.$AppKey)));
|
|
$result = self::shipperPost($ReqURL, $datas);
|
|
$rs = json_decode($result, 1);
|
|
$traces = $rs['Traces'];
|
|
if(count($traces) == 0){
|
|
$traces = [[
|
|
'AcceptTime'=> date("Y-m-d H:i:s"),
|
|
'AcceptStation'=> '暂没有快递信息, 请联系友福客服:4000401707'
|
|
]];
|
|
}
|
|
$data[] = [
|
|
'shipper'=> ShipperModel::where('code',$shipper)->value('name'),
|
|
'shipper_id' => $trackingnum,
|
|
'traces' => $traces,
|
|
'success' => $rs['Success'],
|
|
];
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
//订单识别
|
|
public static function shipperIdentify($id){
|
|
$requestData= "{'LogisticCode':".$id."}";
|
|
$EBusinessID = '1256285';
|
|
$AppKey = '1a4dac6f-7666-4cf4-802e-c503aff437b8';
|
|
$ReqURL = 'http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx';
|
|
|
|
$datas = array(
|
|
'EBusinessID' => $EBusinessID,
|
|
'RequestType' => '2002',
|
|
'RequestData' => urlencode($requestData),
|
|
'DataType' => '2',
|
|
);
|
|
$datas['DataSign'] = urlencode(base64_encode(md5($requestData.$AppKey)));
|
|
$result = self::shipperPost($ReqURL, $datas);
|
|
$data = json_decode($result, true);
|
|
|
|
return $data['Shippers'];
|
|
}
|
|
|
|
/*
|
|
* 授权请求
|
|
*/
|
|
static function http($url, $data, $method)
|
|
{
|
|
$ch = curl_init($url);
|
|
$authorization = "Authorization: Bearer ".config('app.access_token');
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
|
$result = curl_exec($ch);
|
|
curl_close($ch);
|
|
return $result;
|
|
}
|
|
|
|
static function httpV2($url, $data, $method, $header=[])
|
|
{
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
|
$result = curl_exec($ch);
|
|
curl_close($ch);
|
|
return $result;
|
|
}
|
|
|
|
public static function aliyunHttp($url, $body, $method, $common_param)
|
|
{
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $common_param);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
|
$result = curl_exec($ch);
|
|
curl_close($ch);
|
|
return json_decode($result);
|
|
}
|
|
}
|