-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathClient.php
More file actions
731 lines (644 loc) · 21.7 KB
/
Client.php
File metadata and controls
731 lines (644 loc) · 21.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
<?php
/*
* This file is part of the Marketo REST API Client package.
*
* (c) 2014 Daniel Chesterton
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CSD\Marketo;
// Guzzle
use CommerceGuys\Guzzle\Plugin\Oauth2\Oauth2Plugin;
use CSD\Marketo\Response\GetLeadChanges;
use CSD\Marketo\Response\GetPagingToken;
use Guzzle\Common\Collection;
use Guzzle\Service\Client as GuzzleClient;
use Guzzle\Service\Description\ServiceDescription;
// Response classes
use CSD\Marketo\Response\AddOrRemoveLeadsToListResponse;
use CSD\Marketo\Response\AssociateLeadResponse;
use CSD\Marketo\Response\CreateOrUpdateLeadsResponse;
use CSD\Marketo\Response\GetCampaignResponse;
use CSD\Marketo\Response\GetCampaignsResponse;
use CSD\Marketo\Response\GetLeadResponse;
use CSD\Marketo\Response\GetLeadPartitionsResponse;
use CSD\Marketo\Response\GetLeadsResponse;
use CSD\Marketo\Response\GetListResponse;
use CSD\Marketo\Response\GetListsResponse;
use CSD\Marketo\Response\IsMemberOfListResponse;
/**
* Guzzle client for communicating with the Marketo.com REST API.
*
* @link http://developers.marketo.com/documentation/rest/
*
* @author Daniel Chesterton <daniel@chestertondevelopment.com>
*/
class Client extends GuzzleClient
{
/**
* {@inheritdoc}
*/
public static function factory($config = array())
{
$default = array(
'url' => false,
'munchkin_id' => false,
'version' => 1,
'bulk' => false
);
$required = array('client_id', 'client_secret', 'version');
$config = Collection::fromConfig($config, $default, $required);
$url = $config->get('url');
if (!$url) {
$munchkin = $config->get('munchkin_id');
if (!$munchkin) {
throw new \Exception('Must provide either a URL or Munchkin code.');
}
$url = sprintf('https://%s.mktorest.com', $munchkin);
}
$grantType = new Credentials($url, $config->get('client_id'), $config->get('client_secret'));
$auth = new Oauth2Plugin($grantType);
if ($config->get('bulk') === true) {
$restUrl = sprintf('%s/bulk/v%d', rtrim($url, '/'), $config->get('version'));
} else {
$restUrl = sprintf('%s/rest/v%d', rtrim($url, '/'), $config->get('version'));
}
$client = new self($restUrl, $config);
$client->addSubscriber($auth);
$client->setDescription(ServiceDescription::factory(__DIR__ . '/service.json'));
$client->setDefaultOption('headers/Content-Type', 'application/json');
return $client;
}
/**
* This API will return all fields and the data type of a lead object.
* Note: This API returns API field names for both Marketo REST and SOAP APIs.
*
* @param array $args - No required parameters
*
* @link http://developers.marketo.com/documentation/rest/describe/
*
* @return array
*/
public function describeLead($args = array()) {
return $this->getResult('describeLead', $args);
}
/**
* Import Leads via file upload
*
* @param array $args - Must contain 'format' and 'file' keys
* e.g. array( 'format' => 'csv', 'file' => '/full/path/to/filename.csv'
*
* @link http://developers.marketo.com/documentation/rest/import-lead/
*
* @return array
*
* @throws \Exception
*/
public function importLeadsCsv($args)
{
if (!is_readable($args['file'])) {
throw new \Exception('Cannot read file: ' . $args['file']);
}
if (empty($args['format'])) {
$args['format'] = 'csv';
}
return $this->getResult('importLeadsCsv', $args);
}
/**
* Get status of an async Import Lead file upload
*
* @param int $batchId
*
* @link http://developers.marketo.com/documentation/rest/get-import-lead-status/
*
* @return array
*/
public function getBulkUploadStatus($batchId)
{
if (empty($batchId) || !is_int($batchId)) {
throw new \Exception('Invalid $batchId provided in ' . __METHOD__);
}
return $this->getResult('getBulkUploadStatus', array('batchId' => $batchId));
}
/**
* Get failed lead results from an Import Lead file upload
*
* @param int $batchId
*
* @link http://developers.marketo.com/documentation/rest/get-import-failure-file/
*
* @return Guzzle\Http\Message\Response
*/
public function getBulkUploadFailures($batchId)
{
if( empty($batchId) || !is_int($batchId) ) {
throw new \Exception('Invalid $batchId provided in ' . __METHOD__);
}
return $this->getResult('getBulkUploadFailures', array('batchId' => $batchId));
}
/**
* Get warnings from Import Lead file upload
*
* @param int $batchId
*
* @link http://developers.marketo.com/documentation/rest/get-import-warning-file/
*
* @return Guzzle\Http\Message\Response
*/
public function getBulkUploadWarnings($batchId)
{
if( empty($batchId) || !is_int($batchId) ) {
throw new \Exception('Invalid $batchId provided in ' . __METHOD__);
}
return $this->getResult('getBulkUploadWarnings', array('batchId' => $batchId));
}
/**
* Calls the CreateOrUpdateLeads command with the given action.
*
* @param string $action
* @param array $leads
* @param string $lookupField
* @param array $args
*
* @see Client::createLeads()
* @see Client::createOrUpdateLeads()
* @see Client::updateLeads()
* @see Client::createDuplicateLeads()
*
* @link http://developers.marketo.com/documentation/rest/createupdate-leads/
*
* @return CreateOrUpdateLeadsResponse
*/
private function createOrUpdateLeadsCommand($action, $leads, $lookupField, $args, $returnRaw = false)
{
$args['input'] = $leads;
$args['action'] = $action;
if (isset($lookupField)) {
$args['lookupField'] = $lookupField;
}
return $this->getResult('createOrUpdateLeads', $args, false, $returnRaw);
}
/**
* Create the given leads.
*
* @param array $leads
* @param string $lookupField
* @param array $args
* @see Client::createOrUpdateLeadsCommand()
*
* @link http://developers.marketo.com/documentation/rest/createupdate-leads/
*
* @return CreateOrUpdateLeadsResponse
*/
public function createLeads($leads, $lookupField = null, $args = array())
{
return $this->createOrUpdateLeadsCommand('createOnly', $leads, $lookupField, $args);
}
/**
* Update the given leads, or create them if they do not exist.
*
* @param array $leads
* @param string $lookupField
* @param array $args
* @see Client::createOrUpdateLeadsCommand()
*
* @link http://developers.marketo.com/documentation/rest/createupdate-leads/
*
* @return CreateOrUpdateLeadsResponse
*/
public function createOrUpdateLeads($leads, $lookupField = null, $args = array())
{
return $this->createOrUpdateLeadsCommand('createOrUpdate', $leads, $lookupField, $args);
}
/**
* Update the given leads.
*
* @param array $leads
* @param string $lookupField
* @param array $args
* @see Client::createOrUpdateLeadsCommand()
*
* @link http://developers.marketo.com/documentation/rest/createupdate-leads/
*
* @return CreateOrUpdateLeadsResponse
*/
public function updateLeads($leads, $lookupField = null, $args = array())
{
return $this->createOrUpdateLeadsCommand('updateOnly', $leads, $lookupField, $args);
}
/**
* Create duplicates of the given leads.
*
* @param array $leads
* @param string $lookupField
* @param array $args
* @see Client::createOrUpdateLeadsCommand()
*
* @link http://developers.marketo.com/documentation/rest/createupdate-leads/
*
* @return CreateOrUpdateLeadsResponse
*/
public function createDuplicateLeads($leads, $lookupField = null, $args = array())
{
return $this->createOrUpdateLeadsCommand('createDuplicate', $leads, $lookupField, $args);
}
/**
* Get multiple lists.
*
* @param int|array $ids Filter by one or more IDs
* @param array $args
*
* @link http://developers.marketo.com/documentation/rest/get-multiple-lists/
*
* @return GetListsResponse
*/
public function getLists($ids = null, $args = array(), $returnRaw = false)
{
if ($ids) {
$args['id'] = $ids;
}
return $this->getResult('getLists', $args, is_array($ids), $returnRaw);
}
/**
* Get a list by ID.
*
* @param int $id
* @param array $args
*
* @link http://developers.marketo.com/documentation/rest/get-list-by-id/
*
* @return GetListResponse
*/
public function getList($id, $args = array(), $returnRaw = false)
{
$args['id'] = $id;
return $this->getResult('getList', $args, false, $returnRaw);
}
/**
* Get multiple leads by filter type.
*
* @param string $filterType One of the supported filter types, e.g. id, cookie or email. See Marketo's documentation for all types.
* @param string $filterValues Comma separated list of filter values
* @param array $fields Array of field names to be returned in the response
* @param string $nextPageToken
* @link http://developers.marketo.com/documentation/rest/get-multiple-leads-by-filter-type/
*
* @return GetLeadsResponse
*/
public function getLeadsByFilterType($filterType, $filterValues, $fields = array(), $nextPageToken = null, $returnRaw = false)
{
$args['filterType'] = $filterType;
$args['filterValues'] = $filterValues;
if ($nextPageToken) {
$args['nextPageToken'] = $nextPageToken;
}
if (count($fields)) {
$args['fields'] = implode(',', $fields);
}
return $this->getResult('getLeadsByFilterType', $args, false, $returnRaw);
}
/**
* Get a lead by filter type.
*
* Convenient method which uses {@link http://developers.marketo.com/documentation/rest/get-multiple-leads-by-filter-type/}
* internally and just returns the first lead if there is one.
*
* @param string $filterType One of the supported filter types, e.g. id, cookie or email. See Marketo's documentation for all types.
* @param string $filterValue The value to filter by
* @param array $fields Array of field names to be returned in the response
*
* @link http://developers.marketo.com/documentation/rest/get-multiple-leads-by-filter-type/
*
* @return GetLeadResponse
*/
public function getLeadByFilterType($filterType, $filterValue, $fields = array(), $returnRaw = false)
{
$args['filterType'] = $filterType;
$args['filterValues'] = $filterValue;
if (count($fields)) {
$args['fields'] = implode(',', $fields);
}
return $this->getResult('getLeadByFilterType', $args, false, $returnRaw);
}
/**
* Get lead partitions.
*
* @link http://developers.marketo.com/documentation/rest/get-lead-partitions/
*
* @return GetLeadPartitionsResponse
*/
public function getLeadPartitions($args = array(), $returnRaw = false)
{
return $this->getResult('getLeadPartitions', $args, false, $returnRaw);
}
/**
* Get multiple leads by list ID.
*
* @param int $listId
* @param array $args
*
* @link http://developers.marketo.com/documentation/rest/get-multiple-leads-by-list-id/
*
* @return GetLeadsResponse
*/
public function getLeadsByList($listId, $args = array(), $returnRaw = false)
{
$args['listId'] = $listId;
return $this->getResult('getLeadsByList', $args, false, $returnRaw);
}
/**
* Get a lead by ID.
*
* @param int $id
* @param array $fields
* @param array $args
*
* @link http://developers.marketo.com/documentation/rest/get-lead-by-id/
*
* @return GetLeadResponse
*/
public function getLead($id, $fields = null, $args = array(), $returnRaw = false)
{
$args['id'] = $id;
if (is_array($fields)) {
$args['fields'] = implode(',', $fields);
}
return $this->getResult('getLead', $args, false, $returnRaw);
}
/**
* Check if a lead is a member of a list.
*
* @param int $listId List ID
* @param int|array $id Lead ID or an array of Lead IDs
* @param array $args
*
* @link http://developers.marketo.com/documentation/rest/member-of-list/
*
* @return IsMemberOfListResponse
*/
public function isMemberOfList($listId, $id, $args = array(), $returnRaw = false)
{
$args['listId'] = $listId;
$args['id'] = $id;
return $this->getResult('isMemberOfList', $args, is_array($id), $returnRaw);
}
/**
* Get a campaign by ID.
*
* @param int $id
* @param array $args
*
* @link http://developers.marketo.com/documentation/rest/get-campaign-by-id/
*
* @return GetCampaignResponse
*/
public function getCampaign($id, $args = array(), $returnRaw = false)
{
$args['id'] = $id;
return $this->getResult('getCampaign', $args, false, $returnRaw);
}
/**
* Get campaigns.
*
* @param int|array $ids A single Campaign ID or an array of Campaign IDs
* @param array $args
*
* @link http://developers.marketo.com/documentation/rest/get-multiple-campaigns/
*
* @return GetCampaignsResponse
*/
public function getCampaigns($ids = null, $args = array(), $returnRaw = false)
{
if ($ids) {
$args['id'] = $ids;
}
return $this->getResult('getCampaigns', $args, is_array($ids), $returnRaw);
}
/**
* Add one or more leads to the specified list.
*
* @param int $listId List ID
* @param int|array $leads Either a single lead ID or an array of lead IDs
* @param array $args
*
* @link http://developers.marketo.com/documentation/rest/add-leads-to-list/
*
* @return AddOrRemoveLeadsToListResponse
*/
public function addLeadsToList($listId, $leads, $args = array(), $returnRaw = false)
{
$args['listId'] = $listId;
$args['id'] = (array) $leads;
return $this->getResult('addLeadsToList', $args, true, $returnRaw);
}
/**
* Remove one or more leads from the specified list.
*
* @param int $listId List ID
* @param int|array $leads Either a single lead ID or an array of lead IDs
* @param array $args
*
* @link http://developers.marketo.com/documentation/rest/remove-leads-from-list/
*
* @return AddOrRemoveLeadsToListResponse
*/
public function removeLeadsFromList($listId, $leads, $args = array(), $returnRaw = false)
{
$args['listId'] = $listId;
$args['id'] = (array) $leads;
return $this->getResult('removeLeadsFromList', $args, true, $returnRaw);
}
/**
* Delete one or more leads
*
* @param int|array $leads Either a single lead ID or an array of lead IDs
* @param array $args
*
* @link http://developers.marketo.com/documentation/rest/delete-lead/
*
* @return DeleteLeadResponse
*/
public function deleteLead($leads, $args = array(), $returnRaw = false)
{
$args['id'] = (array) $leads;
return $this->getResult('deleteLead', $args, true, $returnRaw);
}
/**
* Trigger a campaign for one or more leads.
*
* @param int $id Campaign ID
* @param int|array $leads Either a single lead ID or an array of lead IDs
* @param array $tokens Key value array of tokens to send new values for.
* @param array $args
*
* @link http://developers.marketo.com/documentation/rest/request-campaign/
*
* @return RequestCampaignResponse
*/
public function requestCampaign($id, $leads, $tokens = array(), $args = array(), $returnRaw = false)
{
$args['id'] = $id;
$args['input'] = array('leads' => array_map(function ($id) {
return array('id' => $id);
}, (array) $leads));
if (!empty($tokens)) {
$args['input']['tokens'] = $tokens;
}
return $this->getResult('requestCampaign', $args, false, $returnRaw);
}
/**
* Schedule a campaign
*
* @param int $id Campaign ID
* @param DateTime $runAt The time to run the campaign. If not provided, campaign will be run in 5 minutes.
* @param array $tokens Key value array of tokens to send new values for.
* @param array $args
*
* @link http://developers.marketo.com/documentation/rest/schedule-campaign/
*
* @return ScheduleCampaignResponse
*/
public function scheduleCampaign($id, \DateTime $runAt = NULL, $tokens = array(), $args = array(), $returnRaw = false)
{
$args['id'] = $id;
if (!empty($runAt)) {
$args['input']['runAt'] = $runAt->format('c');
}
if (!empty($tokens)) {
$args['input']['tokens'] = $tokens;
}
return $this->getResult('scheduleCampaign', $args, false, $returnRaw);
}
/**
* Associate a lead
*
* @param int $id
* @param string $cookie
* @param array $args
*
* @link http://developers.marketo.com/documentation/rest/associate-lead/
*
* @return AssociateLeadResponse
*/
public function associateLead($id, $cookie = null, $args = array(), $returnRaw = false)
{
$args['id'] = $id;
if (!empty($cookie)) {
$args['cookie'] = $cookie;
}
return $this->getResult('associateLead', $args, false, $returnRaw);
}
/**
* Get the paging token required for lead activity and changes
*
* @param string $sinceDatetime String containing a datetime
* @param array $args
* @param bool $returnRaw
*
* @return GetPagingToken
* @link http://developers.marketo.com/documentation/rest/get-paging-token/
*
*/
public function getPagingToken($sinceDatetime, $args = array(), $returnRaw = false)
{
$args['sinceDatetime'] = $sinceDatetime;
return $this->getResult('getPagingToken', $args, false, $returnRaw);
}
/**
* Get lead changes
*
* @param string $nextPageToken Next page token
* @param string|array $fields
* @param array $args
* @param bool $returnRaw
*
* @return GetLeadChanges
* @link http://developers.marketo.com/documentation/rest/get-lead-changes/
* @see getPagingToken
*
*/
public function getLeadChanges($nextPageToken, $fields, $args = array(), $returnRaw = false)
{
$args['nextPageToken'] = $nextPageToken;
$args['fields'] = (array) $fields;
if (count($fields)) {
$args['fields'] = implode(',', $fields);
}
return $this->getResult('getLeadChanges', $args, true, $returnRaw);
}
/**
* Update an editable section in an email
*
* @param int $emailId
* @param string $htmlId
* @param array $args
*
* @link http://developers.marketo.com/documentation/asset-api/update-email-content-by-id/
*
* @return Response
*/
public function updateEmailContent($emailId, $args = array(), $returnRaw = false)
{
$args['id'] = $emailId;
return $this->getResult('updateEmailContent', $args, false, $returnRaw);
}
/**
* Update an editable section in an email
*
* @param int $emailId
* @param string $htmlId
* @param array $args
*
* @link http://developers.marketo.com/documentation/asset-api/update-email-content-in-editable-section/
*
* @return UpdateEmailContentInEditableSectionResponse
*/
public function updateEmailContentInEditableSection($emailId, $htmlId, $args = array(), $returnRaw = false)
{
$args['id'] = $emailId;
$args['htmlId'] = $htmlId;
return $this->getResult('updateEmailContentInEditableSection', $args, false, $returnRaw);
}
/**
* Approve an email
*
* @param int $emailId
* @param string $htmlId
* @param array $args
*
* @link http://developers.marketo.com/documentation/asset-api/approve-email-by-id/
*
* @return approveEmail
*/
public function approveEmail($emailId, $args = array(), $returnRaw = false)
{
$args['id'] = $emailId;
return $this->getResult('approveEmailbyId', $args, false, $returnRaw);
}
/**
* Internal helper method to actually perform command.
*
* @param string $command
* @param array $args
* @param bool $fixArgs
*
* @return Response
*/
private function getResult($command, $args, $fixArgs = false, $returnRaw = false)
{
$cmd = $this->getCommand($command, $args);
// Marketo expects parameter arrays in the format id=1&id=2, Guzzle formats them as id[0]=1&id[1]=2.
// Use a quick regex to fix it where necessary.
if ($fixArgs) {
$cmd->prepare();
$url = preg_replace('/id%5B([0-9]+)%5D/', 'id', $cmd->getRequest()->getUrl());
$cmd->getRequest()->setUrl($url);
}
$cmd->prepare();
if ($returnRaw) {
return $cmd->getResponse()->getBody(true);
}
return $cmd->getResult();
}
}