Problems sending a preview or the campaign using the (restful) PHP-API


this is my php code to create a draft campaign and trying to send a preview / the campaign.

I can create the campaign (it shows up in the createsend backend area) but when trying to send it I get an error for both "send preview" and "send campaign":

The error is:

<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid URL</h2>
<hr><p>HTTP Error 400. The request URL is invalid.</p>

Here is my code:

        $wrap = new CS_REST_Campaigns(NULL, $apiKey, 'https');
        $result = $wrap->create($apiClientId, $listCampaignData);
        if (! $result->was_successful())
            pError('Cannot create newsletter for posting ' . $postingUid . ' for category ' . $categoryUid . ': ' . $result->response);
        $draftId = $result->response;
        pGood('Draft Campaign created: "' . $listCampaignData['Name'] . '" (ID: ' . $draftId . ').');
   // Problems start here...

        // Send preview:
        $wrap = new CS_REST_Campaigns($draftId, $apiKey, 'https');
        $result2 = $wrap->send_preview('', 'Random');
        echo htmlentities(var_export($result2->response, 1)); // THIS GIVES ME THE BAD REQUEST #1
        // Send Newsletter:
        $wrap = new CS_REST_Campaigns($draftId, $apiKey, 'https');
        $result3 = $wrap->send(array("ConfirmationEmail" => '', "SendDate" => "Immediately"));
        echo htmlentities(var_export($result3->response, 1));  // THIS GIVES ME THE BAD REQUEST #2
        if ($result3->was_successful())
            pGood('Scheduled with code ' . $result3->http_status_code);
            pError('Failed with code ' . $result3->http_status_code . "\n" . '<br /><pre>' . var_export($result3->response, 1) . '</pre>');

I do not see what could be wrong. Any ideas?


tvierb, 6 years ago


I have found an "error": the draftId from "$draftId = $result->response;" is enclosed in doublequotes. I trim() it now...

Got another error now when sending the preview:

   'Code' => 400,
   'Message' => 'Failed to deserialize your request. 
Please check the documentation and try again.
Fields in error: info',

I will read the source to find out what that shall mean.

Phil Phil, 6 years ago


It sounds like you're able to send the campaign now, but still having troubles with the preview; is that correct?

Thing is, the addresses you're sending the preview to need to be in an array, even if there's just one.

Try using this instead and see how it goes:

$result2 = $wrap->send_preview(array(''), 'Random');
tvierb, 6 years ago

I finally succeeded in sending a campaign.

Phil Phil, 6 years ago

And the prevew, is that all good?

tobyb tobyb, 6 years ago

Just to follow up, it looks like this behaviour is the result of a specific PHP version. We've updated the wrapper to ensure that the ID's returned from these routes have the surrounding quotes stripped.

See why 200,000 companies worldwide love Campaign Monitor.

From Australia to Zimbabwe, and everywhere in between, companies count on Campaign Monitor for email campaigns that boost the bottom line.

Get started for free