Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,22 @@ public function request($method, $path, $tokens = NULL, $parameters = NULL, $bat
'headers' => [
'Authorization' => $this->api_user . ' ' . $this->api_key,
],
'convert_to_object' => TRUE
];

// Add trigger error header if a debug error code has been set.
if (!empty($this->debug_error_code)) {
$options['headers']['X-Trigger-Error'] = $this->debug_error_code;
}

// Set the object conversion status if its passed in.
if (isset($parameters['convert_to_object'])) {
$options['convert_to_object'] = $parameters['convert_to_object'];

// Remove status from being sent to MailChimp.
unset($parameters['convert_to_object']);
}

if ($this->use_curl) {
return $this->handleRequestCURL($method, $this->endpoint . $path, $options, $parameters, $returnAssoc);
}
Expand All @@ -327,7 +336,12 @@ public function handleRequest($method, $uri = '', $options = [], $parameters = [
}
else {
// Send parameters as JSON in request body.
$options['json'] = (object) $parameters;
if ($options['convert_to_object']) {
$options['json'] = (object) $parameters;
}
else {
$options['json'] = $parameters;
}
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/MailchimpLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,6 @@ public function addSegment($list_id, $name, $parameters = [], $batch = FALSE) {
* The ID of the list.
* @param int $segment_id
* The ID of the segment.
* @param string $name
* The name of the segment.
* @param array $parameters
* Associative array of optional request parameters.
* @param bool $batch
Expand All @@ -398,17 +396,17 @@ public function addSegment($list_id, $name, $parameters = [], $batch = FALSE) {
*
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#edit-patch_lists_list_id_segments_segment_id
*/
public function updateSegment($list_id, $segment_id, $name, $parameters = [], $batch = FALSE) {
public function updateSegment($list_id, $segment_id, $parameters = [], $batch = FALSE) {
$tokens = [
'list_id' => $list_id,
'segment_id' => $segment_id,
];

$parameters += [
'name' => $name,
'convert_to_object' => FALSE
];

return $this->request('PATCH', '/lists/{list_id}/segments/{segment_id}', $tokens, $parameters, $batch);
return $this->request('POST', '/lists/{list_id}/segments/{segment_id}', $tokens, $parameters, $batch);
}

/**
Expand Down
12 changes: 8 additions & 4 deletions tests/MailchimpListsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,23 @@ public function testAddSegment() {
public function testUpdateSegment() {
$list_id = '57afe96172';
$segment_id = '49381';
$name = 'Updated Test Segment';
$emails = array(
'members_to_add' => array(
'[email protected]'
)
);

$mc = new MailchimpLists();
$mc->updateSegment($list_id, $segment_id, $name);
$mc->updateSegment($list_id, $segment_id, $emails);

$this->assertEquals('PATCH', $mc->getClient()->method);
$this->assertEquals('POST', $mc->getClient()->method);
$this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/segments/' . $segment_id, $mc->getClient()->uri);

$this->assertNotEmpty($mc->getClient()->options['json']);

$request_body = $mc->getClient()->options['json'];

$this->assertEquals($name, $request_body->name);
$this->assertEquals('[email protected]', $request_body['members_to_add'][0]);
}

/**
Expand Down