饿了么接口——php

最近项目需要对接饿了么http://merchant.openapi.eleme.io/quickstart.html
饿了么居然没给sdk,只好自己写了,不开心

1.首先要拿到两个参数
consumer_key
是对接饿了么开放平台的基本凭证,它代表的是一个品牌或个人实体。该品牌或个人的所有餐厅会关联在此key之下。
consumer_secret
eleme分配给APP的consumer_key

2.制作签名,直接看代码吧——makeSig2()

3.发送http请求——sendHttp2();

4.制作接口——提供了餐厅接口和订单接口的例子

5.调接口——这就不用写出来了吧

商家测试
饿了么提供了以下体验店信息以供接口开发测试:

consumer_key: 0170804777
consumer_secret: 87217cb263701f90316236c4df00d9352fb1da76

restaurant_id: 62028381
restaurant_name: 饿了么开放平台测试
餐厅下单测试地址: http://r.ele.me/openapi-test

注意:如遇到 "测试餐厅必须与测试帐号一起使用" 的错误提示,请清理cookies

'','consumer_secret'=>'']
     */
    public function __construct($sy_param)
    {
        $this->consumer_key = $sy_param['consumer_key'];
        $this->consumer_secret=$sy_param['consumer_secret'];
    }

    ####################餐厅接口
    /**
     * 查询所属餐厅
     * @return mixed
     */
    function getAllRestaurants(){
        $api_url='restaurant/own/';
        $sys_request =['consumer_key'=>$this->consumer_key,'timestamp'=>time()];
        return self::sendHttp2($api_url,'GET',$sys_request);
    }

    function getRestaurant($restaurant_id){
        $api_url = 'restaurant/'.$restaurant_id.'/';
        $sys_request =['consumer_key'=>$this->consumer_key,'timestamp'=>time()];
        $api_request = array();
        $api_request['restaurant_id'] = $restaurant_id;
        return $this->sendHttp2($api_url,'GET',$sys_request,$api_request);
    }


    #################订单接口

    /**
     * 获取订单详情
     * @param $eleme_order_id       订单编号
     * @param $tp_id                0=不显示第三方ID,1=显示第三方ID
     * @return mixed
     */
    function getOrderDetail($eleme_order_id,$tp_id=null){
        $api_url = 'order/'.$eleme_order_id.'/';
        $sys_request =['consumer_key'=>$this->consumer_key,'timestamp'=>time()];
        $api_request = array();
        $api_request['tp_id'] = $tp_id;
        return $this->sendHttp2($api_url,'GET',$sys_request,$api_request);
    }

    /**
     * 取消订单
     * @param $eleme_order_id
     * @param $reason
     * @return mixed
     */
    function cancelOrder($eleme_order_id,$reason){
        $api_url = 'order/'.$eleme_order_id.'/status/';
        $sys_request =['consumer_key'=>$this->consumer_key,'timestamp'=>time()];
        $api_request =array();
        $api_request['status']=-1;
        $api_request['reason'] = $reason;
        $res = $this->sendHttp2($api_url,'PUT',$sys_request,$api_request);
        return $res;
    }

    /**
     * 确认订单
     * @param $eleme_order_id
     * @return mixed
     */
    function confirmOrder($eleme_order_id){
        $api_url = 'order/'.$eleme_order_id.'/status/';
        $sys_request =['consumer_key'=>$this->consumer_key,'timestamp'=>time()];
        $api_request =array();
        $api_request['status']=2;
        return $this->sendHttp2($api_url,'PUT',$sys_request,$api_request);
    }

    /**
     * 同意退单
     * @param $eleme_order_id
     * @return mixed
     */
    function agreeRefund($eleme_order_id){
        $api_url = 'order/'.$eleme_order_id.'/agree_refund/';
        $sys_request =['consumer_key'=>$this->consumer_key,'timestamp'=>time()];
        return $this->sendHttp2($api_url,'POST',$sys_request);
    }

    /**
     * 不同意退单
     * @param $eleme_order_id
     * @param null $reason
     * @return mixed
     */
    function disagreeRefund($eleme_order_id,$reason=null){
        $api_url = 'order/'.$eleme_order_id.'/disagree_refund/';
        $sys_request =['consumer_key'=>$this->consumer_key,'timestamp'=>time()];
        $api_request =array();
        $api_request['reason']=$reason;
        return $this->sendHttp2($api_url,'POST',$sys_request,$api_request);
    }

    /**
     * 计算签名
     * @param $api_url          指定接口url
     * @param array $params     参数
     * @return string           返回签名
     *
     */
    private function makeSig($api_url,$params=[]){
        //将所有参数(sig除外)按照参数名的字母顺序排序,并用 & 连接:
        if(count($params)){
            ksort($params);
            $str_a = http_build_query($params);
        }else{
            return false;
        }
        var_dump($str_a);
        //按照请求url + ? + 字符串A + consumer_secret的顺序进行连接,得到 字符串B
        $str_b = self::BASE_URL.$api_url.'?'.$str_a.$this->consumer_secret;
        var_dump($str_b);
        //对``字符串B``用UTF-8 Encode之后计算HEX值字符串(用HEX Encode),得到 字符串C:
        $str_c =bin2hex(utf8_encode($str_b));
        var_dump($str_c);
        //对 字符串C 计算SHA1哈希,得到签名:
        $sig = sha1($str_c);
        var_dump($sig);
        return $sig;
    }

    /**
     * @param $api_url
     * @param array $params
     * @return string
     */
    private function makeSig2($api_url,$params=[]){
        ksort($params);
        $pairs = array();
        foreach($params as $key=>$val) {
            array_push($pairs, $key . '=' . urlencode($val));
        }
        $params = join('&', $pairs);
        //var_dump($params);
        $str = self::BASE_URL.$api_url.'?'.$params.$this->consumer_secret;
        $sig = sha1(bin2hex($str));
        //var_dump($sig);
        return $sig;
    }

    /**
     * @param $api_url
     * @param $method
     * @param array $sys_request                系统级参数
     * @param array $api_request                API级参数 可以为空
     * @return mixed
     */
    private function sendHttp2($api_url,$method,$sys_request=[],$api_request=null){
        if(count($api_request)){
            //如果有API级参数
            $api_request = array_filter($api_request);          //删除数组中的空值
            $params = array_merge($sys_request,$api_request);   //API级参数与系统及参数合并得到完整的参数
        }else{
            $params = $sys_request;                             //系统级参数就是完整参数
        }
        $sig = $this->makeSig2($api_url,$params);
        $method = strtoupper($method);
        if($method=='GET'){
            //如果是get方式传参
            $params['sig'] = $sig;
            $url = self::BASE_URL.$api_url.'?'.http_build_query($params);
        }else{
            //如果是其他方式传参,将系统级参事和签名sig,加入到url中即可
            $sys_request['sig'] = $sig;
            $url = self::BASE_URL.$api_url.'?'.http_build_query($sys_request);
        }
        //var_dump($url);
        $ch = curl_init();
        $header = array(
                "Content-Type:application/x-www-form-urlencoded; charset=UTF-8"
        );
        // 添加apikey到header
        $api_request = count($api_request)>0? http_build_query($api_request) :$api_request;
        curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch , CURLOPT_URL , $url);
        switch($method){
            case 'GET':
                curl_setopt($ch,CURLOPT_HTTPGET,true);
                break;
            case 'POST':
                curl_setopt($ch,CURLOPT_POST,true);
                curl_setopt($ch,CURLOPT_POSTFIELDS,$api_request);
                break;
            case 'PUT':
                curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'PUT');
                curl_setopt($ch,CURLOPT_POSTFIELDS,$api_request);
                break;
            case 'DELETE':
                curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'DELETE');
                curl_setopt($ch,CURLOPT_POSTFIELDS,$api_request);
                break;
        }
        // 执行HTTP请求
        $res = curl_exec($ch);

        return $res;
    }
}

你可能感兴趣的:(饿了么接口——php)