Skip to content

Commit

Permalink
Import leads via file upload using Bulk Api
Browse files Browse the repository at this point in the history
  • Loading branch information
dchesterton committed Apr 29, 2015
1 parent 90f23a1 commit e4c8393
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 6 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Setup
The client is built on [Guzzle 3](http://guzzle3.readthedocs.org) and uses a factory method to create an instance.
You must specify either a Munchkin ID or the full url.

####For Rest Api access:
```php
use CSD\Marketo\Client;

Expand All @@ -23,6 +24,18 @@ $client = Client::factory(array(
));
```

####For Bulk Api access:
```php
use CSD\Marketo\Client;

$client = Client::factory(array(
'client_id' => 'Marketo client ID', // required
'client_secret' => 'Marketo client secret', // required
'munchkin_id' => '100-AEK-913' // alternatively, you can supply the full URL, e.g. 'url' => 'https://100-AEK-913.mktorest.com'
'bulk' => true // if uploading leads via file upload (e.g. csv)
));
```

Usage
----------------
View the source of `src/Client.php` for all the available methods.
Expand Down
98 changes: 93 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public static function factory($config = array())
$default = array(
'url' => false,
'munchkin_id' => false,
'version' => 1
'version' => 1,
'bulk' => false
);

$required = array('client_id', 'client_secret', 'version');
Expand All @@ -66,7 +67,11 @@ public static function factory($config = array())
$grantType = new Credentials($url, $config->get('client_id'), $config->get('client_secret'));
$auth = new Oauth2Plugin($grantType);

$restUrl = sprintf('%s/rest/v%d', rtrim($url, '/'), $config->get('version'));
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);
Expand All @@ -76,6 +81,85 @@ public static function factory($config = array())
return $client;
}

/**
* 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.
*
Expand Down Expand Up @@ -215,23 +299,27 @@ public function getList($id, $args = array(), $returnRaw = false)
* @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(), $returnRaw = false)
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.
*
Expand Down
32 changes: 31 additions & 1 deletion src/service.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@
"apiVersion": "1",
"description": "Service description used for talking to the Marketo.com REST API.",
"operations": {
"importLeadsCsv": {
"httpMethod": "POST",
"uri": "leads.json",
"parameters": {
"format": { "location": "postField"},
"file": { "location": "postFile"}
}
},
"getBulkUploadStatus": {
"httpMethod": "GET",
"uri": "leads/batch/{batchId}.json",
"parameters": {
"batchId": {"location": "uri"}
}
},
"getBulkUploadFailures": {
"httpMethod": "GET",
"uri": "leads/batch/{batchId}/failures.json",
"parameters": {
"batchId": {"location": "uri"}
}
},
"getBulkUploadWarnings": {
"httpMethod": "GET",
"uri": "leads/batch/{batchId}/warnings.json",
"parameters": {
"batchId": {"location": "uri"}
}
},
"getLists": {
"httpMethod": "GET",
"uri": "lists.json",
Expand Down Expand Up @@ -40,7 +69,8 @@
"parameters": {
"filterType": {"location": "query"},
"filterValues": {"location": "query"},
"fields": {"location": "query"}
"fields": {"location": "query"},
"nextPageToken": {"location": "query", "required": false}
},
"responseClass": "CSD\\Marketo\\Response\\GetLeadsResponse"
},
Expand Down

0 comments on commit e4c8393

Please sign in to comment.