2

I'm trying to integrate the MailChimp API 3.0 with PHP into a custom built CMS. When I try to create a single campaign with the following code I have an error message.

CODE:

$apikey = <--api-key-->;
$list_id = <--list-id-->;

use \DrewM\MailChimp\MailChimp;

$MailChimp = new MailChimp($apikey);

$result = $MailChimp->post("campaigns", [
                'type'              => 'regular',
                'recipients'        => ['list_id'           =>  '$list_id'],
                'settings'          => ['subject_line'      =>  'test subject', 
                                        'reply_to'          =>  'test@test.com',
                                        'from_name'         =>  'test name'],

            ]);

echo "<pre>";
print_r($result);
print_r($MailChimp->getLastRequest());
echo "</pre>";

ERROR MESSAGE:

Array
(
[type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/
[title] => Invalid Resource
[status] => 400
[detail] => The resource submitted could not be validated. For field-specific details, see the 'errors' array.
[instance] => 
[errors] => Array
    (
        [0] => Array
            (
                [field] => 
                [message] => Schema describes object, NULL found instead
            )

    )

)

USING print_r($MailChimp->getLastRequest());

Array
(
[method] => post
[path] => campaigns
[url] => https://us8.api.mailchimp.com/3.0/campaigns
[body] => 
[timeout] => 10
[headers] => POST /3.0/campaigns HTTP/1.0
User-Agent: DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)
Host: us8.api.mailchimp.com
Accept-Encoding: deflate, gzip
Accept: application/vnd.api+json
Content-Type: application/vnd.api+json
Authorization: apikey <--api-key-->
Content-Length: 0
)

I have checked the documentation, searched through SO, but haven't found anything. If anyone has a working code that creates a simple campaign I can start working with, I would appreciate it. What am I missing?

balintpekker
  • 1,804
  • 4
  • 28
  • 42
  • Is MailChimp part of an official distributed PHP Rest Client for MailChip's API ? Or an self developed class? – Dragos Dec 09 '16 at 13:38
  • You can find it [here](https://github.com/drewm/mailchimp-api/), it's in MailChimp's documentation you can use it. – balintpekker Dec 09 '16 at 13:40
  • as it is written there, you can use `print_r($MailChimp->getLastRequest());` to check the request you are sending. There is definetly something wrong with the Request getting sent to the REST API. – Dragos Dec 09 '16 at 13:49
  • I edited my question with the `getLastRequest()` function, I don't really know what to look for in it... – balintpekker Dec 09 '16 at 13:55
  • The body of your request is NULL. This is the reason of the error. The MailChimp API is expecting in the Body a json objec with the structure you already saw in their documentation. – Dragos Dec 09 '16 at 14:04
  • Thank you for your help, I achieved it somehow, will share the answer! – balintpekker Dec 09 '16 at 14:18

1 Answers1

1

This is the code I used to get it done, also check for the encoding, it can cause problems

$result = $MailChimp->post("campaigns", [
                           "type" =>"regular",
                               "recipients"=> [
                                   "list_id"=> "$list_id"
                                   ]
                               ,
                               "settings"=> [
                                   "subject_line"=> "test subject",
                                   "from_name"=> "test name",
                                   "reply_to"=> "test@test.com"
                               ]
                           ]);
balintpekker
  • 1,804
  • 4
  • 28
  • 42