Skip to content

Commit

Permalink
mdf:refactor# 优化抓取物流信息和新增代理IP选项抓取
Browse files Browse the repository at this point in the history
  • Loading branch information
uuk020 committed May 16, 2019
1 parent 3fd7f80 commit 61af80b
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 249 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ sftp-config.json
composer.lock
.subsplit
.php_cs.cache
.idea/
62 changes: 6 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,40 +88,10 @@ $logistics->query('12313131231', ['ickd']);
]
```

## 百度接口获取物流信息
```php
$logistics->query('12313131231', 'baidu');
$logistics->query('12313131231', ['baidu']);
```
示例:

```php
[
'baidu' => [
'channel' => 'baidu',
'status' => 'success',
'result' => [
[
'status' => 200,
'message' => 'OK',
'error_code' => 0,
'data' => [
['time' => '2019-01-09 12:11', 'description' => '仓库-已签收'],
['time' => '2019-01-09 12:11', 'description' => '广东XX服务点'],
['time' => '2019-01-09 12:11', 'description' => '广东XX转运中心']
],
'logistics_company' => '申通快递',
'logistics_bill_no' => '12312211'
]
]
]
]
```

## 多接口获取物流信息
```php
$logistics->query('12313131231');
$logistics->query('12313131231', ['kuaidi100', 'ickd', 'baidu']);
$logistics->query('12313131231', ['kuaidi100', 'ickd']);
```
示例:

Expand Down Expand Up @@ -170,53 +140,33 @@ $logistics->query('12313131231', ['kuaidi100', 'ickd', 'baidu']);
'logistics_bill_no' => '12312211'
]
]
],
'baidu' => [
'channel' => 'baidu',
'status' => 'success',
'result' => [
[
'status' => 200,
'message' => 'OK',
'error_code' => 0,
'data' => [
['time' => '2019-01-09 12:11', 'description' => '仓库-已签收'],
['time' => '2019-01-09 12:11', 'description' => '广东XX服务点'],
['time' => '2019-01-09 12:11', 'description' => '广东XX转运中心']
],
'logistics_company' => '申通快递',
'logistics_bill_no' => '12312211'
]
]
]
]
```

## 参数说明
```
array query(string $code, $channels = ['kuaidi100'])
array queryByProxy(array $proxy, string $code, $channels = ['kuaidi100'])
```

* query 与 queryByProxy 返回数组结构是一样, 只是多了一个参数代理IP
* $proxy - 代理地址 结构: ['proxy' => '代理IP:代理端口']
* $code - 运单号
* $channel - 渠道名称, 可选参数,默认快递 100.目前支持百度(baidu), 快递 100 (kuaidi 100), 爱查快递(ickd)

## 有关请求次数
据测试随机生成 100 个运单循环请求, 目前没有限制. 但这些接口都是我抓包得来, 虽然我随机设置 user-agent,
但是不够保险, 因此可能请求过多会有限制,就要更换 IP 来去请求.
请求次数过于频繁, Kudidi100会封IP, 而ickd则不会, 但会返回错误信息, 假如需要请求多次, 则需要代理IP. 我试着抓免费代理IP
基本上没一个可以用, 即使有用, 但存活时间很短. 因此只增加代理IP参数.

## 参考
* [PHP 扩展包实战教程 - 从入门到发布](https://laravel-china.org/courses/creating-package)
* [高德开放平台接口的 PHP 天气信息组件(weather)](https://github.com/overtrue/weather)
* [满足你的多种发送需求的短信发送组件(easy-sms)](https://github.com/overtrue/easy-sms)

## 最后
感谢安正超 - 超哥提供教程, 让我知道如何构建一个包, 学习到很多东西.
其实我才做 PHP 没多久, 没想到有这么多人 star.
虽然现在才 121,但对我来说是一份认可, 谢谢.

欢迎提出 issue 和 pull request


## License

MIT
117 changes: 0 additions & 117 deletions src/Channel/BaiduChannel.php

This file was deleted.

45 changes: 43 additions & 2 deletions src/Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,67 @@
* Time: 21:32
*/

declare(strict_types = 1);
namespace Wythe\Logistics\Channel;

use Wythe\Logistics\Exceptions\HttpException;
use Wythe\Logistics\Traits\HttpRequest;

abstract class Channel
{
/**
* HTTP 请求
*/
use HttpRequest;

/**
* 渠道URL
*
* @var string
*/
protected $url;

/**
* 请求资源
*
* @var array
*/
protected $response;

/**
* 请求选项
*
* @var array
*/
protected $option = [];

/**
* 设置请求选项
*
* @param array $option
* @return \Wythe\Logistics\Channel\Channel
*/
public function setRequestOption(array $option): Channel
{
if (!empty($this->option)) {
if (isset($option['header']) && isset($this->option['header'])) {
$this->option['header'] = array_merge($this->option['header'], $option['header']);
}
if (isset($option['proxy'])) {
$this->option['proxy'] = $option['proxy'];
}
} else {
$this->option = $option;
}
return $this;
}

/**
* 调用查询接口
*
* @param string $code
* @return array
*/
abstract public function get(string $code):array ;
abstract public function request(string $code):array ;

/**
* 转换为数组
Expand Down
12 changes: 7 additions & 5 deletions src/Channel/IckdChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
* Date: 2019/1/9
* Time: 22:09
*/

declare(strict_types = 1);
namespace Wythe\Logistics\Channel;


use Wythe\Logistics\Exceptions\HttpException;

class IckdChannel extends Channel
{
protected $option = ['header' => ['referer: https://biz.trace.ickd.cn']];

public function __construct()
{
$this->url = 'https://biz.trace.ickd.cn/auto/';
Expand Down Expand Up @@ -41,7 +42,7 @@ private function randCode(): string
* @return array
* @throws \Wythe\Logistics\Exceptions\HttpException
*/
public function get(string $code): array
public function request(string $code): array
{
try {
$urlParams = [
Expand All @@ -53,8 +54,9 @@ public function get(string $code): array
'callback' => '_jqjsp',
'_'.time()
];
$response = $this->request($this->url.$code, $urlParams, 0, ['referer: https://biz.trace.ickd.cn']);
$this->format($this->toArray($response));
$response = $this->get($this->url.$code, $urlParams, $this->option);
$this->toArray($response);
$this->format();
return $this->response;
} catch (\Exception $exception) {
throw new HttpException($exception->getMessage());
Expand Down
14 changes: 6 additions & 8 deletions src/Channel/Kuaidi100Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
* Date: 2018/12/24
* Time: 21:37
*/

declare(strict_types = 1);
namespace Wythe\Logistics\Channel;


use Wythe\Logistics\Exceptions\Exception;
use Wythe\Logistics\Exceptions\HttpException;
use Wythe\Logistics\Exceptions\InvalidArgumentException;

class Kuaidi100Channel extends Channel
{
Expand All @@ -34,7 +31,7 @@ public function __construct()
* @return array
* @throws \Wythe\Logistics\Exceptions\HttpException
*/
public function get(string $code): array
public function request(string $code): array
{
try {
$companyCodes = $this->getCompanyCode($code);
Expand All @@ -43,8 +40,9 @@ public function get(string $code): array
$urlParams[] = ['type' => $companyCode, 'postid' => $code];
$urls[] = $this->url;
}
$response = $this->requestWithUrls($urls, $urlParams);
$this->format($this->toArray($response));
$response = $this->getByQueue($urls, $urlParams, $this->option);
$this->toArray($response);
$this->format();
return $this->response;
} catch (\Exception $exception) {
throw new HttpException($exception->getMessage());
Expand All @@ -61,7 +59,7 @@ public function get(string $code): array
protected function getCompanyCode(string $code): array
{
$params = ['resultv2' => 1, 'text' => $code];
$response = $this->request($this->autoGetCompanyNameByUrl, $params);
$response = $this->get($this->autoGetCompanyNameByUrl, $params);
$getCompanyInfo = \json_decode($response, true);
if (!isset($getCompanyInfo['auto'])) {
throw new HttpException('no company code');
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Date: 2018/12/24
* Time: 21:31
*/

declare(strict_types = 1);
namespace Wythe\Logistics\Exceptions;


Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Date: 2018/12/24
* Time: 21:31
*/

declare(strict_types = 1);
namespace Wythe\Logistics\Exceptions;


Expand Down
Loading

0 comments on commit 61af80b

Please sign in to comment.