【问题标题】:In Laravel How to create subscriber in mailchimp using Guzzle HTTP在 Laravel 中如何使用 Guzzle HTTP 在 mailchimp 中创建订阅者
【发布时间】:2018-04-28 18:16:20
【问题描述】:

我正在使用 guzzle HTTP 发送请求,但出现此错误 客户端错误:POST https://us17.api.mailchimp.com/3.0/batches 导致 401 Unauthorized 响应: {"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"API Key Missing","statu (truncated...)

        $userArray = [];
        $operations = [];
        //used to get the patient that need to sync mailchimp
        $getPatientToSync = Patient::select('NameFirst', 'NameLast' , 'Email')      
        ->where([['flag_name' , '=', '1'], ['mailchimp_synced' ,'=' , '0']])->get();
        if($getPatientToSync->isEmpty()){
            return true;
        }
        foreach ($getPatientToSync as  $patient){
            $data = array(
                "apikey"        => config('mailchimp.api_key'),
                "email_address" => $patient->Email,
                "status"        => "subscribed",
                "merge_fields"  => array(
                    'FNAME' => $patient->NameFirst,
                    'LNAME' => $patient->NameLast,
                )
            );
            $userArray[] =  json_encode($data);
           }
        foreach ($userArray as $userArr){
            $temp = array(
                "method" => "POST",
                "path" => "/lists/".config('mailchimp.list_id')."/members/",
                "body" => $userArr
            );
            $operations['operations'][] =$temp;
        }
        $json_post = json_encode($operations);
        $auth = base64_encode( 'user:'.config('mailchimp.api_key') );
        //API URL
        $urll="https://".config('mailchimp.data_center').".api.mailchimp.com/3.0/batches";
       $headers = array('Content-Type: application/json', 'Authorization: Basic 
        '.$auth , $userlist);
        $client = new Client();
        $response =  $client->request('POST', $urll , $headers,json_post );
        dd($response);

【问题讨论】:

    标签: php laravel-5 guzzle


    【解决方案1】:

    首先,你错误地使用了 Guzzle 的request() 方法。正确的做法是:

    $client->request('POST', $urll, [
        'headers' => $headers,
        'json' => $operations
    ]);
    

    尝试一下,然后注意错误信息(这也很有用)。

    【讨论】:

      猜你喜欢
      • 2019-06-14
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      相关资源
      最近更新 更多