Latest

Offshoot Blog

Using Zend_Soap_Client with the Campaign Monitor API

I recently needed to write a PHP client to integrate with the SOAP side of the Campaign Monitor API. The code was pretty simple and straight-forward, but I was getting a weird error: 101 Invalid ListID. I checked and re-checked my ListID in our Campaign Monitor account and verified that I was using the correct ListID. Why then was I getting this Invalid ListID error?

After reading the Campaign Monitor API documentation a couple times, I realized that there was a specific header that I wasn’t using:

SOAPAction: "http://api.createsend.com/api/Subscriber.AddWithCustomFields"

Due to the Invalid ListID error, I wasn’t even thinking about headers! To fix the problem, all I needed to add was one line of code:

$client->addSoapInputHeader(
        new SoapHeader($soapUri, 'SOAPAction', $soapUri . $action)
    );

Then everything worked! Awesome.

Here’s a full code snippet:

 
$wsdl = 'http://api.createsend.com/api/api.asmx?wsdl';
$soapUri = 'http://api.createsend.com/api/';
$apiUri = 'http://api.createsend.com/api/api.asmx';
 
$apiKey = 'xxxxxxxxxxxxxxxxx'; // replace with your API key!
$listId = 'xxxxxxxxxxxxxxxxx'; // replace with a valid ListID! ;)
 
$action = 'Subscriber.Add';
		$method = 'AddSubscriber';
 
$client = new Zend_Soap_Client($wsdl);
$client->addSoapInputHeader(
        new SoapHeader($soapUri, 'SOAPAction', $soapUri . $action)
    );
 
$params = array ('ApiKey' => $apiKey,
		 'ListID' => $listId,
		 'Email' => '[email protected]',
		 'Name' => 'Test Testford');
 
$response = $client->$method($params);
 
$resultName = $action . 'Result';
 
print_r($response->$resultName);

3 Responses to Using Zend_Soap_Client with the Campaign Monitor API

  1. Marc Guyer says:

    Hey Chris — Thanks for posting this info. I’m working on a ZF compatible wrapper for CM SOAP interaction but I continually get the same error: 101 Invalid ListID. Were you really able to get this to work? I can’t even get this to work: http://api.createsend.com/api/api.asmx?op=Subscriber.Add

  2. @Marc, have you confirmed that you’ve properly set an HTTP header for the SOAP Action? In this case,
    SOAPAction: “http://api.createsend.com/api/Subscriber.Add”. It needs to be added through your SOAP Client.

    I’m also assuming you’ve verified that the ListID you’re posting to is correct. :)

    I have been able to add subscribers successfully using the code above

  3. Marc Guyer says:

    Chris — I didn’t completely RTFM of course. I ended up writing a nice little wrapper that you might be interested in. http://blog.sproutbox.com/2010/02/24/campaignmonitor-api-is-annoying/

*
To prove that you're not a bot, enter this code
Anti-Spam Image

67 Mowat Suite 307 Toronto, Ontario M6K 3E3 Canada
p: 1.416.656.3362 / 1.866.656.3362 | f: 416.656.7236
© 2012 Offshoot Inc. All Rights Reserved