PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 6, 2022

[FIXED] How to get single record, filtered by email address, from MailChimp list using API 3.0

 September 06, 2022     json, mailchimp, mailchimp-api-v3.0, php     No comments   

Issue

Flowing new subscribers from our CRM into MailChimp was easy, but what I need to do now is to get a single customer record based on only their email address and fetch their MailChimp ID from the MailChimp API.

The MailChimp API 3.0 documentation has limited examples of using filters for GET calls. I would think it would be possible to filter the results without retrieving the entire member list, which is what the script is doing now, but I just need that one customer record, not the entire list else I receive the entire JSON string of every member of the list, encode this massive string into an array then I need to iterate through the JSON array to find the record with the matching email address, which seems very inefficient to me. I MUST be missing something that's not easily found in the MailChimp API 3.0 docs, no?!

I am using the PHP library, but it doesn't matter, I really just need to know how to build the API call -- the proper URL (can I append with ?email_address=EMAIL_TO_FILTER tried it but didn't work) and the JSON array structure. I have a successful connection with the MailChimp API and built a mailChimpApiRequest() method like so:

function mailChimpApiRequest($method='POST', $url, $postFields = null){

    $ch  = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);

    $headerArr=array(   'Authorization: '  . $this->apikey);

    if($method!='POST' && !empty($method)) {
      //Not needed: $headerArr[] = 'X-HTTP-Method-Override: ' . $method;
    }

    curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $method );
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headerArr );
    curl_setopt($ch, CURLOPT_POST, true);

    if ($postFields) {
        if ($method!=''){    $postFields['method']=$method;}

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postFields));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    }

    $curl_response = curl_exec($ch);
    curl_close($ch);

    return $curl_response;
}

Can somebody help me figure out what $url and $postFields array to pass to this mailChimpApiRequest() method above?


Solution

Just append the URL with the MD5 of email address in all lowercase. Too easy, I missed that in the MailChimp API docs.



Answered By - Aaron Belchamber
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing