PHP版本美团闪购开放平台sdk

对接美团闪购开放平台

美团闪购开放平台只有java的sdk,特此封装php板sdk
美团闪购平台开发文档:https://open-shangou.meituan.com/home/doc/market/11

class MeituanShangou
{
    protected $appId = 'xxx';
    protected $appSecret = 'xxx';
    protected $systemParam;
    protected $applicationParam;
    protected $url;
    protected $requestMethod;

    /**
     * MeituanShangou constructor.
     * @param $url
     * @param string $requestMethod
     * @param array $applicationParam
     */
    public function __construct($url, $requestMethod = 'get', $applicationParam = [])
    {
        $this->systemParam = [
            'app_id' => $this->appId,
            'app_secret' => $this->appSecret,
            'timestamp' => time()
        ];
        $this->url = $url;
        $this->requestMethod = $requestMethod;
        $this->applicationParam = $applicationParam;
    }

    /**
     * 设置appId
     * @param $appId
     *
     * @return void
     * @throws
     */
    public function setAppId($appId)
    {
        $this->appId = $appId;
    }

    /**
     * 设置appSecret
     * @param $appSecret
     *
     * @return void
     * @throws
     */
    public function setAppSecret($appSecret)
    {
        $this->appSecret = $appSecret;
    }

    /**
     * 获得签名
     *
     * @return string
     * @throws
     */
    public function getSign()
    {
        $url = $this->url;
        $systemParam = $this->systemParam;
        $applicationParam = $this->applicationParam;

        $allParam = array_merge($systemParam, $applicationParam);
        ksort($allParam);
        $str = '';
        foreach ($allParam as $key => $val) {
            if ($key != 'app_secret') {
                $str .= '&' . $key . '=' . $val;
            }
        }
        $str = ltrim($str, '&');
        $url = $url . '?' . $str . $systemParam['app_secret'];

        $sign = md5($url);

        $strToUtf8 = function ($str) {
            $encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
            if($encode == 'UTF-8'){
                return $str;
            }else{
                return mb_convert_encoding($str, 'UTF-8', $encode);
            }
        };

        return $strToUtf8($sign);
    }

    /**
     * 获得最终地址
     * @return string
     * @throws
     */
    public function genUrl()
    {
        $requestMethod = $this->requestMethod;
        $url = $this->url;
        $sign = $this->getSign();
        $systemParam = $this->systemParam;
        $applicationParam = $this->applicationParam;

        if (strtolower($requestMethod) == 'get') {
            $uriParamStr = 'app_id=' . $systemParam['app_id'] . '&timestamp=' . $systemParam['timestamp'];
            foreach ($applicationParam as $key => $val) {
                $uriParamStr .= '&' . $key . '=' . $val;
            }
            $baseUrl = $url . '?' . $uriParamStr . '&sig=' . $sign;
            return $baseUrl;
        } else {
            ksort($systemParam);
            $str = '';
            foreach ($systemParam as $key => $val) {
                if ($key != 'app_secret') {
                    $str .= '&' . $key . '=' . $val;
                }
            }
            $baseUrl = $url . '?' . $str . '&sig=' . $sign;
            return $baseUrl;
        }
    }

    /**
     * curl 请求
     * @param $url url请求地址
     * @param $param post参数
     * @param $method 请求方法
     *
     * @return mixed
     * @throws
     */
    public function curlRequest($url, $param = [], $method = 'get')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);//URL 地址
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//FALSE禁止cURL验证对等证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//设置为1是检查服务器SSL证书中是否存在一个公用名
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//TRUE 将curl_exec()获取的信息以字符串返回,而不是直接输出。
        curl_setopt($ch, CURLOPT_TIMEOUT, 20);//超时设置,以秒为单位

        if (strtolower($method) == 'post') {
            curl_setopt($ch, CURLOPT_POST, TRUE);//TRUE时会发送POST请求,类型为:application/x-www-form-urlencoded,是 HTML 表单提交时最常见的一种
            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);//全部数据使用HTTP协议中的 "POST" 操作来发送
        }

        $output = curl_exec($ch);//执行命令

        $result = [
            'status' => true,
            'code' => 200,
            'message' => '',
            'data' => []
        ];

        if (curl_error($ch)) {
            $result['status'] = false;
            $result['code'] = 0;
            $result['message'] = 'Error: ' . curl_error($ch);
        } else {

            $result['data'] = json_decode($output,true);
        }

        curl_close($ch);

        return $result;
    }
}


//使用示例
function example()
{
	//获取应用已绑定门店的三方门店ID
    $url = 'https://waimaiopen.meituan.com/api/v1/poi/getids';
    $requestMethod = 'get';
    $applicationParam = [];
	
	//门店设置为营业状态
	$url = 'https://waimaiopen.meituan.com/api/v1/poi/open';
	$requestMethod = 'post';
	$applicationParam = ['app_poi_code' => '5681_2701618'];

    $mtsg = new MeituanShangou($url, $requestMethod, $applicationParam);
    $baseUrl = $mtsg->genUrl();
    $result = $mtsg->curlRequest($baseUrl, $applicationParam, $requestMethod);
    var_dump($result);
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值