MailChimp API DrewM\MailChimp\MailChimp::call() 调用未定义的方法

Posted

技术标签:

【中文标题】MailChimp API DrewM\\MailChimp\\MailChimp::call() 调用未定义的方法【英文标题】:MailChimp API DrewM\MailChimp\MailChimp::call() call to undefined methodMailChimp API DrewM\MailChimp\MailChimp::call() 调用未定义的方法 【发布时间】:2018-07-01 23:20:41 【问题描述】:

我正在尝试验证 apikey,但如果用户输入一个 apikey,它应该从 MailChimp 帐户进行验证,并且如果 api 密钥与 MailChimp 不匹配,则应该显示一条无效密钥的消息。

我的代码:

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());
        $mc = new MailChimp($api);
        $mailchimp_ping = ($mc->call('helper/ping'));
        if(!$mailchimp_ping)
        
        $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);

当我打这个电话时,它说call to aundefined 方法是否还有其他我可以验证 apikey 的方法。 任何帮助将不胜感激!

               namespace App\Http\Controllers;

                    use App\APIKEY;
                 use DrewM\MailChimp\MailChimp;
                use Illuminate\Auth\Access\Response;
                  use Illuminate\Http\Request;
                use Illuminate\Support\Facades\Auth;
                  use Illuminate\Support\Facades\DB;
                 use Illuminate\Support\Facades\Input;
              use Illuminate\Support\Facades\Mail;
            use Illuminate\Support\Facades\Validator;


          class ApiController extends Controller
         

           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());
        $mc = new MailChimp($api);
        $mailchimp_ping = $mc->get('/ping');
        if ($mailchimp_ping["status"] == 401) 
            return redirect()->back()->with("errorMessage","api key is invalid")->withInput(Input::all());
        


        ///do here if api key is valid

        $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);
    

【问题讨论】:

【参考方案1】:

创建一个request。如果请求有效,则密钥为valid。按照this 链接发出ping 请求。这是MailChimp API 3.0 的健康检查端点。

$mailchimp_ping = $mc->get('/ping');

dd($mailchimp_ping);

这是您请求的可以正常工作的完整控制器-

<?php

namespace App\Http\Controllers;

use App\APIKEY;
use DrewM\MailChimp\MailChimp;
use Illuminate\Auth\Access\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;


class ApiController extends Controller


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 
        $mc = new MailChimp($request->input('api_key'));
        $mailchimp_ping = $mc->get('/ping');
        if ($mailchimp_ping["status"] != 401) 
             $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);
        

          $errorResponse = [
                 'message' => 'Not valid api key!',
                 'code' => '401'
               ];
        return $this->apiResponse('error', '401', $errorResponse);

    


【讨论】:

你能不能给我一个小演示如何提出请求我是 mailchimp 的新手,我被困在这个问题上,任何帮助将不胜感激。 向我们展示您是如何向 mailchip 提出请求的,这是可行的 $mailchimp = 新 \Mailchimp($api); $mailchimp_ping = ($mailchimp->call('helper/ping')); if(!$mailchimp_ping) //key 没用 //do stuff 这里没有任何工作你可以看到上面我提出请求的代码 $api = new APIKEY($request->all()); $request->user()->apikey()->save($api); $resultArray = ['status' => 1, 'message' => 'Mailchimp Api key 添加到系统成功!', 'dataArray' => $api];

以上是关于MailChimp API DrewM\MailChimp\MailChimp::call() 调用未定义的方法的主要内容,如果未能解决你的问题,请参考以下文章

Mailchimp 标记 API 静默失败

Mailchimp 如何在 javascript 中调用 mailchimp 3.0 API

通过 MailChimp 2.0 API 为用户订阅群组

Mailchimp api 3.0错误:“Schema描述对象,找到数组”是代码还是在mailchimp的结尾?

只需一次调用 MailChimp API v3 即可获取所有 mailchimp 活动的详细信息

Mailchimp API 3.0 批量订阅 - Mailchimp3 PY