PHP code example of finecho / meituan
1. Go to this page and download the library: Download finecho/meituan library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
finecho / meituan example snippets
$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' => '深圳市南山区',
]
);
use EasyMeiTuan\Application;
$app = new Application($config);
$api = $app->getClient();
$response = $api->poi->save->post(
[
'name' => 'finecho 的快餐店',
'address' => '深圳市南山区',
]
);
$server = $app->getServer();
// url:在美团外卖设置的回调地址
// content:美团外卖推送过来的内容, 在美团外卖开放平台配置回调地址美团服务器发起验证码时 content 为空数组
$server->withUrl($url)->with(
function ($content) {
// ...
}
);
return $server->serve();
// 默认需要解码字段以及规则
\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',
];
$app->store->$method();
$app->deliveryRange->$method();
$app->category->$method();
$app->product->$method();
$app->order->$method();
$app->refund->$method();
$app->logistic->$method();
$app->crowdSourcing->$method();
$app->common->$method();
// 获取状态码
$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');
// 请求是否正常
$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;
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) {
// 捕获到表单异常
}