如何在laravel 5.5中验证Mailchimp api密钥

时间:2018-01-23 05:45:23

标签: laravel mailchimp-api-v3.0

我想验证我的mailchimp apikey如果用户输入错误的api密钥,则应显示错误的mailchimp api消息或异常:

我的代码是:

                 public function authenticateApiKey(Request 
                 $request)
{
    $fieldsValidation = [

        'api_key' => 'required|unique:apikey,api_key',

    ];
    $validator = Validator::make($request->all(), $fieldsValidation);

    if ($validator->fails()) {
        $resultArray = [
            'status' => 0,
            'message' => $validator->errors()->first(),
            'dataArray' => []
        ];
    } else {
        $api = new APIKEY($request->all());

        $request->user()->apikey()->save($api);
        $resultArray = ['status' => 1, 'message' => 'Mailchimp Api key added into system successfully!', 'dataArray' => $api];
    }
    return \Illuminate\Support\Facades\Response::json($resultArray, 200);
}

我根据登录用户保存我的api密钥,在此代码中,api密钥未验证。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

试试这个:

else {
        $api = new APIKEY($request->all());
        $mailchimp = new \Mailchimp( $api);

       $mailchimp_ping = ($mailchimp->call('helper/ping'));

       if(!$mailchimp_ping){
            //key did not work
            //do stuff
       }

        $request->user()->apikey()->save($api);
        $resultArray = ['status' => 1, 'message' => 'Mailchimp Api key added into system successfully!', 'dataArray' => $api];
    }

如果API密钥无效,则上述call将返回false;如果是有效密钥,则返回以下内容:

array(1) { ["msg"]=> string(20) "Everything's Chimpy!" }