Full code: https://github.com/kenpeter/test_guzzle
I try to create a mailchimp list with Guzzle, the http client.
When I am running php create_list_memeber_update.php
Error I got is:
PHP Fatal error: Uncaught InvalidArgumentException: Invalid resource type: array in /var/www/html/test/testme/test_guzzle/vendor/guzzlehttp/psr7/src/functions.php:116
Stack trace:
#0 /var/www/html/test/testme/test_guzzle/vendor/guzzlehttp/psr7/src/Request.php(53): GuzzleHttp\Psr7\stream_for(Array)
#1 /var/www/html/test/testme/test_guzzle/create_list_memeber_update.php(49): GuzzleHttp\Psr7\Request->__construct('POST', Object(GuzzleHttp\Psr7\Uri), Array, Array)
#2 {main}
thrown in /var/www/html/test/testme/test_guzzle/vendor/guzzlehttp/psr7/src/functions.php on line 116
I think it is something to do with the headers
$headers = array(
'User-Agent' => 'testing/1.0',
'Accept' => 'application/json'
);
Code for create_list_memeber_update.php:
<?php
// auto load
require 'vendor/autoload.php';
use GuzzleHttp\Psr7\Request;
// opt
$option = array(
'base_uri' => "https://us12.api.mailchimp.com/3.0/",
'auth' => ['apikey', '292bae37c631ac3ba03ed0640b44e6c3'],
);
// client
$client = new \GuzzleHttp\Client($option);
// data for a new list
$data = array(
"name" => "test_mailchimp",
"contact" => array(
"company" => "MailChimp",
"address1" => "675 Ponce De Leon Ave NE",
"address2" => "Suite 5000",
"city" => "Atlanta",
"state" => "GA",
"zip" => "30308",
"country" => "US",
"phone" => "12345678",
),
"permission_reminder" => "You're receiving this email because you signed up for updates.",
"use_archive_bar" => true,
"campaign_defaults" => array(
"from_name" => "test",
"from_email" => "test@test.com",
"subject" => "test_subject",
"language" => "en",
),
"notify_on_subscribe" => "",
"notify_on_unsubscribe" => "",
"email_type_option" => true,
"visibility" => "pub",
);
$headers = array(
'User-Agent' => 'testing/1.0',
'Accept' => 'application/json'
);
$body = ['json' => $data];
$req_create_list = new Request('POST', 'lists', $headers, $body);
// promise
$promise = $client
->sendAsync($req_create_list)
->then(function ($res) {
echo $res->getBody();
});
$promise->wait();
This is the solution I found and its working perfectly fine
// format for mail chimp data
--url 'https://usX.api.mailchimp.com/3.0/batches' \
--user 'anystring:apikey' \
--header 'content-type: application/json' \
--data '{"operations" : [{"method" : "POST","path" :
"lists/624ea08019/members", "body": "
{\"email_address\":\"freddie@mailchimp.com\",
\"status\":\"subscribed\"}"},
//end format
$userlist = array(format array data)
$auth = base64_encode('user:'.config('mailchimp.api_key'));
//API URL
$urll = "https://".config('mailchimp.data_center').".api.mailchimp.com/3.0/batches";
//API authentication Header
$headers = array(
'Accept' => 'application/json',
'Authorization' => 'Basic '.$auth
);
$client = new Client();
$req_Memeber = new Request('POST', $urll, $headers, $userlist);
// promise
$promise = $client->sendAsync($req_Memeber)->then(function ($res){
echo "Synched";
});
$promise->wait();
It seems you are passing the $body value as array, however the Guzzle Request class expects the fourth parameter to be an object. Try using json_encode to fix that:
$config['auth']=['apikey',env('MAILCHIMP_API_KEY')];
$config['base_uri']=env('MAILCHIMP_API_URL');
$client = new Client($config);
$url = '3.0/lists/'.$listId.'/members';
$headers['content-type']='application/json';
$data=['email_address'=>$email,'status'=>'subscribed','segment'=>$segment];
$json = json_encode($data);
$request = new Request('POST', $url, $headers, $json);