diff --git a/README.md b/README.md index 89273be..6cbf3d8 100755 --- a/README.md +++ b/README.md @@ -1,14 +1,14 @@ # Constant Contact PHP SDK [![Build Status](https://secure.travis-ci.org/constantcontact/php-sdk.png?branch=master)](http://travis-ci.org/constantcontact/php-sdk) [![Latest Stable Version](https://poser.pugx.org/constantcontact/constantcontact/v/stable.svg)](https://packagist.org/packages/constantcontact/constantcontact) [![Latest Unstable Version](https://poser.pugx.org/constantcontact/constantcontact/v/unstable.svg)](https://packagist.org/packages/constantcontact/constantcontact) -### This library utilizes [GuzzlePHP](https://guzzlephp.org) +### This library utilizes [GuzzlePHP](http://guzzle.readthedocs.org/) ## Installing via Composer [Composer](https://getcomposer.org/) is a dependency management tool for PHP that allows you to declare the dependencies your project needs and installs them into your project. In order to use the Constant Contact PHP SDK through composer, you must add "constantcontact/constantcontact" as a dependency in your project's composer.json file. ```javascript { "require": { - "constantcontact/constantcontact": "2.0.*" + "constantcontact/constantcontact": "2.1.*" } } ``` diff --git a/examples/addOrUpdateContact.php b/examples/addOrUpdateContact.php index e31f16b..02a02e8 100644 --- a/examples/addOrUpdateContact.php +++ b/examples/addOrUpdateContact.php @@ -70,19 +70,25 @@ } else { $action = "Updating Contact"; - $contact = Contact::create($response->results[0]); - $contact->addList($_POST['list']); - $contact->first_name = $_POST['first_name']; - $contact->last_name = $_POST['last_name']; - - /* - * The third parameter of updateContact defaults to false, but if this were set to true it would tell - * Constant Contact that this action is being performed by the contact themselves, and gives the ability to - * opt contacts back in and trigger Welcome/Change-of-interest emails. - * - * See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in - */ - $returnContact = $cc->contactService->updateContact(ACCESS_TOKEN, $contact); + $contact = $response->results[0]; + if ($contact instanceof Contact) { + $contact->addList($_POST['list']); + $contact->first_name = $_POST['first_name']; + $contact->last_name = $_POST['last_name']; + + /* + * The third parameter of updateContact defaults to false, but if this were set to true it would tell + * Constant Contact that this action is being performed by the contact themselves, and gives the ability to + * opt contacts back in and trigger Welcome/Change-of-interest emails. + * + * See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in + */ + $returnContact = $cc->contactService->updateContact(ACCESS_TOKEN, $contact); + } else { + $e = new CtctException(); + $e->setErrors(array("type", "Contact type not returned")); + throw $e; + } } // catch any exceptions thrown during the process and print the errors to screen diff --git a/src/Ctct/Components/EmailMarketing/CampaignPreview.php b/src/Ctct/Components/EmailMarketing/CampaignPreview.php new file mode 100644 index 0000000..6f26f70 --- /dev/null +++ b/src/Ctct/Components/EmailMarketing/CampaignPreview.php @@ -0,0 +1,51 @@ +fromEmail = parent::getValue($props, "from_email"); + $preview->replyToEmail = parent::getValue($props, "reply_to_email"); + $preview->htmlContent = parent::getValue($props, "preview_email_content"); + $preview->textContent = parent::getValue($props, "preview_text_content"); + $preview->subject = parent::getValue($props, "subject"); + return $preview; + } +} \ No newline at end of file diff --git a/src/Ctct/Services/ContactService.php b/src/Ctct/Services/ContactService.php index 87a25eb..fdb042e 100755 --- a/src/Ctct/Services/ContactService.php +++ b/src/Ctct/Services/ContactService.php @@ -194,6 +194,8 @@ public function updateContact($accessToken, Contact $contact, Array $params = ar $query->add($name, $value); } } + $stream = Stream::factory(json_encode($contact)); + $request->setBody($stream); try { $response = parent::getClient()->send($request); diff --git a/src/Ctct/Services/EmailMarketingService.php b/src/Ctct/Services/EmailMarketingService.php index 668848a..d3b7e3c 100755 --- a/src/Ctct/Services/EmailMarketingService.php +++ b/src/Ctct/Services/EmailMarketingService.php @@ -4,6 +4,7 @@ use Ctct\Exceptions\CtctException; use Ctct\Util\Config; use Ctct\Components\EmailMarketing\Campaign; +use Ctct\Components\EmailMarketing\CampaignPreview; use Ctct\Components\ResultSet; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Stream\Stream; @@ -85,13 +86,13 @@ public function getCampaigns($accessToken, Array $params = array()) /** * Get campaign details for a specific campaign * @param string $accessToken - Constant Contact OAuth2 access token - * @param int $campaign_id - Valid campaign id + * @param int $campaignId - Valid campaign id * @return Campaign * @throws CtctException */ - public function getCampaign($accessToken, $campaign_id) + public function getCampaign($accessToken, $campaignId) { - $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign'), $campaign_id); + $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign'), $campaignId); $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl); @@ -107,13 +108,13 @@ public function getCampaign($accessToken, $campaign_id) /** * Delete an email campaign * @param string $accessToken - Constant Contact OAuth2 access token - * @param int $campaign_id - Valid campaign id + * @param int $campaignId - Valid campaign id * @return boolean * @throws CtctException */ - public function deleteCampaign($accessToken, $campaign_id) + public function deleteCampaign($accessToken, $campaignId) { - $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign'), $campaign_id); + $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign'), $campaignId); $request = parent::createBaseRequest($accessToken, 'DELETE', $baseUrl); @@ -149,4 +150,24 @@ public function updateCampaign($accessToken, Campaign $campaign) return Campaign::create($response->json()); } + + /** + * Get a preview of an email campaign + * @param string $accessToken - Constant Contact OAuth2 access token + * @param int $campaignId - Valid campaign id + * @return CampaignPreview + * @throws CtctException + */ + public function getPreview($accessToken, $campaignId) { + $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign_preview'), $campaignId); + + $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl); + try { + $response = parent::getClient()->send($request); + } catch (ClientException $e) { + throw parent::convertException($e); + } + + return CampaignPreview::create($response->json()); + } } diff --git a/src/Ctct/Util/Config.php b/src/Ctct/Util/Config.php index 866a138..fdadeae 100755 --- a/src/Ctct/Util/Config.php +++ b/src/Ctct/Util/Config.php @@ -38,6 +38,7 @@ class Config 'campaign_schedules' => 'emailmarketing/campaigns/%s/schedules', 'campaign_schedule' => 'emailmarketing/campaigns/%s/schedules/%s', 'campaign_test_sends' => 'emailmarketing/campaigns/%s/tests', + 'campaign_preview' => 'emailmarketing/campaigns/%s/preview', 'campaign_tracking_summary' => 'emailmarketing/campaigns/%s/tracking/reports/summary', 'campaign_tracking_bounces' => 'emailmarketing/campaigns/%s/tracking/bounces', 'campaign_tracking_clicks' => 'emailmarketing/campaigns/%s/tracking/clicks', @@ -124,7 +125,7 @@ class Config * Setting the version fo the application used in Rest Calls when setting the version header */ 'settings' => array( - 'version' => '2.0.0' + 'version' => '2.1.0' ), ); diff --git a/test/Json/Campaigns/get_preview.json b/test/Json/Campaigns/get_preview.json new file mode 100644 index 0000000..45b6ff6 --- /dev/null +++ b/test/Json/Campaigns/get_preview.json @@ -0,0 +1,7 @@ +{ + "subject":"Subject Test", + "from_email":"myemail@example.com", + "reply_to_email":"myemail@example.com", + "preview_email_content":"
View this message as a web page\nClick here\n
As a reminder, you're receiving this email because you have expressed an interest in MyCompany. Don't forget to add from_email@example.com to your address book so we'll be sure to land in your inbox! You may unsubscribe if you no longer wish to receive our emails.
 
You may unsubscribe if you no longer wish to receive our emails.

This is text of the email message.


\n\n\n
\n\n\n\n\n\n
Click here to forward this message
\n
\n
\n
This email was sent to {Email Address} by rmarcucella@constantcontact.com |  
\n
\n

My Organization | 123 Maple Street | Suite 1 | Anytown | MA | 01444
\n
\n
\n
\n</body>", + "preview_text_content":"View this message as a web page\nClick here\nhttp://campaign.r20.l1.constantcontact.com/render?ca=025eff86-6378-4f53-9301-5897ecf50b30&c={Contact Id}&ch={Contact Id}\n\nAs a reminder, you're receiving this email because you have expressed an interest in MyCompany. Don't forget to add from_email@example.com to your address book so we'll be sure to land in your inbox! You may unsubscribe if you no longer wish to receive our emails. You may unsubscribe\nhttp://visitor.l1.constantcontact.com/do?p=un&m=001JZtDyxcvPiye1EthMqSLGA%3D%3D&ch={Contact Id}&ca=025eff86-6378-4f53-9301-5897ecf50b30\n if you no longer wish to receive our emails.\n------------------------------------------------------------\nThis is the text of the email message.\n\nClick here to forward this message\nhttp://ui.l1.constantcontact.com/sa/fwtf.jsp?llr=cqmhk9aab&m=1100394770946&ea=rmarcucella%40constantcontact.com&a=1100400205633\n\n\n\n\n\nThis email was sent to {Email Address} by rmarcucella@constantcontact.com.\n\nUpdate Profile/Email Address\nhttp://visitor.l1.constantcontact.com/do?p=oo&m=001JZtDyxcvPiye1EthMqSLGA%3D%3D&ch={Contact Id}&ca=025eff86-6378-4f53-9301-5897ecf50b30\n\n\nInstant removal with SafeUnsubscribe(TM)\nhttp://visitor.l1.constantcontact.com/do?p=un&m=001JZtDyxcvPiye1EthMqSLGA%3D%3D&ch={Contact Id}&ca=025eff86-6378-4f53-9301-5897ecf50b30\n\n\nPrivacy Policy:\nhttp://ui.l1.constantcontact.com/roving/CCPrivacyPolicy.jsp\n\n\n\n\n\nOnline Marketing by\nhttp://img.l1.constantcontact.com/letters/images/cc-logo-color-sm.gif\nhttp://www.constantcontact.com\n\n\n\nMy Organization | 123 Maple Street | Suite 1 | Anytown | MA | 01444\n\n\n\n\n\n\n\n\n" +} diff --git a/test/Json/JsonLoader.php b/test/Json/JsonLoader.php index 1261fc7..39de9b4 100755 --- a/test/Json/JsonLoader.php +++ b/test/Json/JsonLoader.php @@ -106,6 +106,10 @@ public static function getTestSendJson() return file_get_contents(__DIR__ . self::CAMPAIGNS_FOLDER . "/post_test_send.json"); } + public static function getPreviewJson() { + return file_get_contents(__DIR__ . self::CAMPAIGNS_FOLDER . "/get_preview.json"); + } + public static function getClicks() { return file_get_contents(__DIR__ . self::CAMPAIGN_TRACKING_FOLDER . "/get_clicks.json"); diff --git a/test/Services/EmailCampaignServiceUnitTest.php b/test/Services/EmailCampaignServiceUnitTest.php index 2b99fa9..ac2407e 100755 --- a/test/Services/EmailCampaignServiceUnitTest.php +++ b/test/Services/EmailCampaignServiceUnitTest.php @@ -2,6 +2,7 @@ use Ctct\Components\ResultSet; use Ctct\Components\EmailMarketing\Campaign; +use Ctct\Components\EmailMarketing\CampaignPreview; use GuzzleHttp\Client; use GuzzleHttp\Exception\ClientException; @@ -21,13 +22,15 @@ public static function setUpBeforeClass() self::$client = new Client(); $getCampaignsStream = Stream::factory(JsonLoader::getCampaignsJson()); $getCampaignStream = Stream::factory(JsonLoader::getCampaignJson()); + $getPreviewStream = Stream::factory(JsonLoader::getPreviewJson()); $mock = new Mock([ new Response(200, array(), $getCampaignsStream), new Response(204, array()), new Response(400, array()), new Response(200, array(), $getCampaignStream), new Response(201, array(), $getCampaignStream), - new Response(200, array(), $getCampaignStream) + new Response(200, array(), $getCampaignStream), + new Response(200, array(), $getPreviewStream) ]); self::$client->getEmitter()->attach($mock); } @@ -272,4 +275,17 @@ public function testUpdateCampaign() $this->assertEquals("1100394163874", $campaign->click_through_details[0]->url_uid); $this->assertEquals(10, $campaign->click_through_details[0]->click_count); } + + public function testGetPreview() { + $response = self::$client->get('/'); + + $preview = CampaignPreview::create($response->json()); + $this->assertEquals("Subject Test", $preview->subject); + $this->assertEquals("myemail@example.com", $preview->fromEmail); + $this->assertEquals("myemail@example.com", $preview->replyToEmail); + $htmlContent = "
View this message as a web page\nClick here\n
As a reminder, you're receiving this email because you have expressed an interest in MyCompany. Don't forget to add from_email@example.com to your address book so we'll be sure to land in your inbox! You may unsubscribe if you no longer wish to receive our emails.
 
You may unsubscribe if you no longer wish to receive our emails.

This is text of the email message.


\n\n\n
\n\n\n\n\n\n
Click here to forward this message
\n
\n
\n
This email was sent to {Email Address} by rmarcucella@constantcontact.com |  
\n
\n

My Organization | 123 Maple Street | Suite 1 | Anytown | MA | 01444
\n
\n
\n
\n</body>"; + $this->assertEquals($htmlContent, $preview->htmlContent); + $textContent = "View this message as a web page\nClick here\nhttp://campaign.r20.l1.constantcontact.com/render?ca=025eff86-6378-4f53-9301-5897ecf50b30&c={Contact Id}&ch={Contact Id}\n\nAs a reminder, you're receiving this email because you have expressed an interest in MyCompany. Don't forget to add from_email@example.com to your address book so we'll be sure to land in your inbox! You may unsubscribe if you no longer wish to receive our emails. You may unsubscribe\nhttp://visitor.l1.constantcontact.com/do?p=un&m=001JZtDyxcvPiye1EthMqSLGA%3D%3D&ch={Contact Id}&ca=025eff86-6378-4f53-9301-5897ecf50b30\n if you no longer wish to receive our emails.\n------------------------------------------------------------\nThis is the text of the email message.\n\nClick here to forward this message\nhttp://ui.l1.constantcontact.com/sa/fwtf.jsp?llr=cqmhk9aab&m=1100394770946&ea=rmarcucella%40constantcontact.com&a=1100400205633\n\n\n\n\n\nThis email was sent to {Email Address} by rmarcucella@constantcontact.com.\n\nUpdate Profile/Email Address\nhttp://visitor.l1.constantcontact.com/do?p=oo&m=001JZtDyxcvPiye1EthMqSLGA%3D%3D&ch={Contact Id}&ca=025eff86-6378-4f53-9301-5897ecf50b30\n\n\nInstant removal with SafeUnsubscribe(TM)\nhttp://visitor.l1.constantcontact.com/do?p=un&m=001JZtDyxcvPiye1EthMqSLGA%3D%3D&ch={Contact Id}&ca=025eff86-6378-4f53-9301-5897ecf50b30\n\n\nPrivacy Policy:\nhttp://ui.l1.constantcontact.com/roving/CCPrivacyPolicy.jsp\n\n\n\n\n\nOnline Marketing by\nhttp://img.l1.constantcontact.com/letters/images/cc-logo-color-sm.gif\nhttp://www.constantcontact.com\n\n\n\nMy Organization | 123 Maple Street | Suite 1 | Anytown | MA | 01444\n\n\n\n\n\n\n\n\n"; + $this->assertEquals($textContent, $preview->textContent); + } }