Reputation: 113
The mailgun API documentation suggest using the following script for adding users to a mailing list via curl:
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/lists/LIST@YOUR_DOMAIN_NAME/members \
-F subscribed=True \
-F address='[email protected]' \
-F name='Bob Bar' \
-F description='Developer' \
-F vars='{"age": 26}'
I am trying to rewrite this so that it works with PHP:
$data = json_encode(array(
"subscribed" => "True",
"address" => "[email protected]",
"name" => "Bob Bar"
));
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.mailgun.net/v3/lists/LIST@YOUR_DOMAIN_NAME/members');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:{YOUR_API_KEY}");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
curl_close($curl);
print_r($result);
I am obviously replacing both https://api.mailgun.net/v3/lists/LIST@YOUR_DOMAIN_NAME/members
and YOUR_API_KEY
with the appropriate strings, however it is failing. Can anyone see where I am going wrong?
Many thanks.
Upvotes: 3
Views: 2485
Reputation: 113
Resolved using the following code:
$data = array(
"subscribed" => "True",
"address" => "[email protected]",
"name" => "Bob Bar"
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.mailgun.net/v3/lists/LIST@YOUR_DOMAIN_NAME/members');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:YOUR_API_KEY");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
curl_close($curl);
print_r($result);
Hope this helps someone.
Upvotes: 3