This repository has been archived by the owner on Sep 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Php example #25
Open
nekhbet
wants to merge
3
commits into
mailchimp:master
Choose a base branch
from
nekhbet:PHP-Exam
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Php example #25
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# MailChimp API v3.0 Examples for PHP | ||
|
||
These examples have been tested with PHP version 5.6 and requires the cURL library. | ||
|
||
Place your API Key in the example.php file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
require_once(__DIR__.'/mailchimp.php'); | ||
|
||
// Init mailchimp | ||
$mailchimp = new MailChimp('YOUR API KEY'); | ||
|
||
$reply = $mailchimp->list_add_subscriber([ | ||
'id_list' => 'ID_LIST', | ||
'email' => 'EMAIL', | ||
'status' => 'STATUS', | ||
'merge_fields' => [ | ||
'FNAME' => 'FIRST NAME', | ||
], | ||
]); | ||
|
||
echo '<pre>'; | ||
print_r($reply); | ||
echo '</pre>'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
class MailChimpException extends Exception { }; | ||
|
||
/** | ||
* Class MailChimp | ||
*/ | ||
|
||
class MailChimp { | ||
|
||
private $api_uri; | ||
private $api_user = 'api_v3'; | ||
private $api_key = ''; | ||
private $user_agent = 'API V3 - PHP Sample'; | ||
|
||
public function __construct($api_key) | ||
{ | ||
$domain = explode('-', $api_key)[1]; | ||
$this->api_uri = 'https://' . $domain . '.api.mailchimp.com/3.0'; | ||
$this->api_key = $api_key; | ||
} | ||
|
||
private function _execute_request($method, $endpoint, Array $payload = []) | ||
{ | ||
$endpoint = $this->api_uri.$endpoint; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest spaces around the concatenation operator to be consistent with code in other parts of this app:
|
||
|
||
$ch = curl_init(); | ||
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | ||
curl_setopt($ch, CURLOPT_USERPWD, $this->api_user.':'.$this->api_key); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In reference to the use of a constant rather than a class property. Change to:
|
||
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | ||
curl_setopt($ch, CURLOPT_TIMEOUT, 10); | ||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); | ||
|
||
switch ($method) | ||
{ | ||
case 'DELETE': | ||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); | ||
break; | ||
case 'GET': | ||
$endpoint .= '?' . http_build_query($payload); | ||
break; | ||
case 'DELETE': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You listed the |
||
$endpoint .= '?' . http_build_query($payload); | ||
break; | ||
case 'POST': | ||
curl_setopt($ch, CURLOPT_POST, TRUE); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); | ||
break; | ||
case 'PUT': | ||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); | ||
break; | ||
} | ||
|
||
curl_setopt($ch, CURLOPT_URL, $endpoint); | ||
$result = curl_exec($ch); | ||
|
||
return json_decode($result); | ||
} | ||
|
||
/** | ||
* List: Add a new subscriber | ||
* @docs http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/ | ||
* @param array $options | ||
* @return mixed | ||
* @throws MailChimpException | ||
* | ||
* Usage Example: | ||
* $obj->list_add_subscriber([ | ||
* 'id_list' => 'ID_LIST', // Required | ||
* 'email' => 'EMAIL', // Required | ||
* 'status' => 'STATUS', // Optional | ||
* 'merge_fields' => [ // Optional | ||
* 'FNAME' => 'NAME', | ||
* ... | ||
* ], | ||
* ]); | ||
* | ||
*/ | ||
public function list_add_subscriber(Array $options) | ||
{ | ||
// Check required params | ||
if ( ! isset($options['id_list']) OR ! isset($options['email'])) | ||
{ | ||
throw new MailChimpException('Parameters not set on '.__METHOD__); | ||
} | ||
|
||
$endpoint = '/lists/' . $options['id_list'] . '/members/'; | ||
$payload = [ | ||
'email_address' => $options['email'], | ||
'status' => isset($options['status']) ? $options['status'] : 'pending', | ||
]; | ||
if (isset($options['merge_fields']) AND $options['merge_fields']) | ||
{ | ||
$payload['merge_fields'] = $options['merge_fields']; | ||
} | ||
|
||
return $this->_execute_request('POST', $endpoint, $payload); | ||
} | ||
|
||
|
||
/** | ||
* Campaign: Send a campaign | ||
* @docs http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#action-post_campaigns_campaign_id_actions_send | ||
* @param array $options | ||
* @return mixed | ||
* @throws MailChimpException | ||
* | ||
* Usage Example: | ||
* $obj->campaign_send([ | ||
* 'id_campaign' => 'ID_CAMPAIGN', // Required | ||
* ]); | ||
* | ||
*/ | ||
public function campaign_send(Array $options) | ||
{ | ||
// Check required params | ||
if ( ! isset($options['id_campaign'])) | ||
{ | ||
throw new MailChimpException('Parameters not set on '.__METHOD__); | ||
} | ||
|
||
$endpoint = '/campaigns/' . $options['id_campaign'] . '/actions/send/'; | ||
|
||
return $this->_execute_request('POST', $endpoint); | ||
} | ||
} | ||
|
||
?> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering this value should never change at run time, it should probably be a class constant:
Access with:
Reference: