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:
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' => 'firstname.lastname@example.org', 'Name' => 'Test Testford'); $response = $client->$method($params); $resultName = $action . 'Result'; print_r($response->$resultName);