finecho/meituan
Composer 安装命令:
composer require finecho/meituan
包简介
美团开放平台SDK
README 文档
README
美团开放平台 SDK
温馨提示
⚠️ 目前仅支持美团外卖服务
安装
环境要求:
- PHP >= 8.0
composer require finecho/meituan -vvv
配置
$config = [ // 必填,app_id、secret_id 'app_id' => 10020201024, 'secret_id' => 'AKIDsiQzQla780mQxLLU2GJCxxxxxxxxxxx', // 是否开启表单验证 'form_verify' => false, ];
使用
您可以使用三种调用方式:封装方式调用、原始方式调用 和 链式调用,请根据你的喜好自行选择使用方式,效果一致。
方式一 - 封装方式调用
use EasyMeiTuan\Application; $app = new Application($config); $response = $app->store->create( [ 'name' => 'finecho 的快餐店', 'address' => '深圳市南山区', ] ); // 也可以这样 $response = $app->store->create( [ 'body' => [ 'name' => 'finecho 的快餐店', 'address' => '深圳市南山区', ], 'headers' => [], ] );
方式二 - 原始方式调用
use EasyMeiTuan\Application; $app = new Application($config); $api = $app->getClient(); $response = $api->post( '/poi/save', [ 'name' => 'finecho 的快餐店', 'address' => '深圳市南山区', ] );
方式三 - 链式调用
你可以将需要调用的 API 以 / 分割 + 驼峰写法的形式,写成如下模式:
use EasyMeiTuan\Application; $app = new Application($config); $api = $app->getClient(); $response = $api->poi->save->post( [ 'name' => 'finecho 的快餐店', 'address' => '深圳市南山区', ] );
表单校验
如果开启表单校验,如果参数缺失或者异常,则会抛出 InvalidParamsException 异常
美团推送
在接收美团推送的时候,Server 会对签名进行校验,并返回解码后的内容
$server = $app->getServer(); // url:在美团外卖设置的回调地址 // content:美团外卖推送过来的内容, 在美团外卖开放平台配置回调地址美团服务器发起验证码时 content 为空数组 $server->withUrl($url)->with( function ($content) { // ... } ); return $server->serve();
签名校验的时候, 需要将已编码的字段内容进行解码,SDK 提供属性可自行配置 decode 规则。
- url:对值进行
urldecode - json:为对值进行
json_decode(val, true)
// 默认需要解码字段以及规则 \EasyMeiTuan\Server::$casts = [ 'caution' => 'url', 'detail' => 'url|json', 'extras' => 'url|json', 'recipient_name' => 'url', 'wm_poi_address' => 'url', 'recipient_address' => 'url', 'incmp_modules' => 'url|json', 'order_tag_list' => 'url|json', 'backup_recipient_phone' => 'url|json', 'recipient_address_desensitization' => 'url', // FBI Warning: nested content needs to pay attention to the order! 'poi_receive_detail_yuan' => 'url|json', 'poi_receive_detail_yuan.reconciliationExtras' => 'json', 'poi_receive_detail' => 'url|json', 'poi_receive_detail.reconciliationExtras' => 'json', ];
API
API 接口众多,每一个 API 都会注释上美团文档地址,查询困难时,可以直接搜索匹配。
🌐 门店
$app->store->$method();
🚚 配送范围
具体方法:src/Services/DeliveryRange.php
$app->deliveryRange->$method();
📝 类目
具体方法:src/Services/Category.php
$app->category->$method();
🍻 菜品
$app->product->$method();
📄 订单
$app->order->$method();
🗑️ 订单退款
$app->refund->$method();
🚚 订单配送
具体方法:src/Services/Logistic.php
$app->logistic->$method();
📦 众包
具体方法:src/Services/CrowdSourcing.php
$app->crowdSourcing->$method();
🔧 全局公共
$app->common->$method();
返回值
API Client 基于 symfony/http-client 实现,你可以通过以下方式对响应值进行访问:
// 获取状态码 $statusCode = $response->getStatusCode(); // 获取全部响应头 $headers = $response->getHeaders(); // 获取响应原始内容 $content = $response->getContent(); // 获取 json 转换后的数组格式 $content = $response->toArray(); // 将内容转换成 Stream 返回 $content = $response->toStream(); // 获取其他信息,如:"response_headers", "redirect_count", "start_time", "redirect_url" 等. $httpInfo = $response->getInfo(); // 获取指定信息 $startTime = $response->getInfo('start_time'); // 获取请求日志 $httpLogs = $response->getInfo('debug');
在原有 Response 的基础上,增加了以下几种方法:
// 请求是否正常 $isSuccess = $response->isSuccess(): bool; // 请求是否出现异常 $hasError = $response->hasError(): bool; // 获取错误内容(code + msg) $error = $response->getError(): array; // 获取错误信息 $error = $response->getErrorMsg(): ?string; // 获取错误码 $error = $response->getErrorCode(): string|int|null; // 获取正常返回的数据 $data = $response->getData(): mixed;
一个比较完整的示例
require __DIR__ .'/vendor/autoload.php'; use EasyMeiTuan\Application; use EasyMeiTuan\Exceptions\InvalidParamsException; $config = [ 'app_id' => 'xxx', 'secret_id' => 'xxxxxxxxxxx', 'form_verify' => true, ]; $app = new Application($config); try { $response = $app->store->list(); if ($response->hasError()) { $error = $response->getError(); // ..... } $data = $response->getData(); // .... } catch (InvalidParamsException $e) { // 捕获到表单异常 }
License
MIT
finecho/meituan 适用场景与选型建议
finecho/meituan 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58 次下载、GitHub Stars 达 45, 最近一次更新时间为 2021 年 11 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 finecho/meituan 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 finecho/meituan 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 58
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 45
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-15