From 2517bf253866fcfa7bd20815fd2cff8303e491c1 Mon Sep 17 00:00:00 2001 From: qq958691165 <958691165@qq.com> Date: Fri, 6 May 2022 12:11:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86guzzle=E5=BA=93=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E8=87=B37.0=E4=BB=A5=E4=B8=8A=20(#10)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 将guzzle库升级至7.0以上 添加crawler.php至composer可执行脚本 在crawler.php添加OUTPUT_DIR可自定义输出目录 * a * a --- composer.json | 7 +++++-- scripts/crawler.php | 10 ++++++++-- src/Client.php | 3 +-- 3 files changed, 14 insertions(+), 6 deletions(-) mode change 100755 => 100644 scripts/crawler.php diff --git a/composer.json b/composer.json index ec9de2f..a95e0b7 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "library", "require": { "php": "^7.1|^8.0", - "guzzlehttp/guzzle": "^6.0", + "guzzlehttp/guzzle": "^7.0", "spatie/array-to-xml": "^2.7" }, "license": "MIT", @@ -21,5 +21,8 @@ }, "require-dev": { "phpunit/phpunit": "^9.0" - } + }, + "bin": [ + "scripts/crawler.php" + ] } diff --git a/scripts/crawler.php b/scripts/crawler.php old mode 100755 new mode 100644 index 9b54243..681d8f1 --- a/scripts/crawler.php +++ b/scripts/crawler.php @@ -5,10 +5,16 @@ use GuzzleHttp\Exception\BadResponseException; use ChinaDivisions\Exceptions\ResponseException; -require __DIR__ . '/../vendor/autoload.php'; +if ($GLOBALS['_composer_autoload_path']){ + require $GLOBALS['_composer_autoload_path']; +}else { + require __DIR__ . '/../vendor/autoload.php'; +} define('MAX_LEVEL', getenv('MAX_LEVEL') ?: 4); // 1.国家 -> 2.省 -> 3.市 -> 4.区县 -> 5.街道乡镇 define('CHILDREN_KEY', getenv('CHILDREN_KEY') ?: 'children'); // 子节点键名 +define('OUTPUT_DIR', getenv('OUTPUT_DIR') ?: __DIR__.'/output'); + const DIVISION_FILTER = 'division_filter'; function division_filter(array $data): ?array @@ -69,6 +75,6 @@ function division_filter(array $data): ?array } file_put_contents( - sprintf('%s/output/level%s.json', __DIR__, MAX_LEVEL), + sprintf('%s/level%s.json', OUTPUT_DIR, MAX_LEVEL), json_encode($collection, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) ); diff --git a/src/Client.php b/src/Client.php index e9d3f83..383da7d 100644 --- a/src/Client.php +++ b/src/Client.php @@ -6,7 +6,6 @@ use GuzzleHttp\Client as HttpClient; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\BadResponseException; -use function GuzzleHttp\Psr7\build_query; use GuzzleHttp\Psr7\Request; use Psr\Http\Message\RequestInterface; use Spatie\ArrayToXml\ArrayToXml; @@ -53,7 +52,7 @@ public function build($msgType, $logisticProviderId, $logisticsInterface, $salt) $dataDigest = $this->signer->make($logisticsInterface, $salt); - $body = build_query([ + $body = http_build_query([ 'msg_type' => $msgType, 'logistic_provider_id' => $logisticProviderId, 'logistics_interface' => $logisticsInterface,