I found a bug in the PHP SDK and have a bug fix....


I found a bug in the PHP SDK and have a bug fix....

I just spent way too much time figuring this out. But I figured it out....


I was trying to add a contact with action_by and opt_in_source set to ACTION_BY_VISITOR, but it wasn't letting me. Constant Contact's servers kept saying "amp;action_by" is an invalid parameter. I'll spare you the 8 hours I spent trying to fix it by posting the instructions here. Hopefully this makes it into the official build.


In the SDK, open up BaseService.php and go to line 47. You will see this function:

    public function buildUrl($url, $queryParams = null)
        $keyArr = array('api_key' => $this->apiKey);
        if ($queryParams) {
            $params = array_merge($keyArr, $queryParams);
        } else {
            $params = $keyArr;

        return $url . '?' . http_build_query($params);

The problem with this function is certain versions of PHP will return an encoded string ready to be inserted into HTML. This means all parameters are separated by & instead of a plain & symbol. This causes problems with the servers. The solution is simple. We just need to tell http_build_query to build a query with & instead of &


Replace line 56 with this:

return $url . '?' . http_build_query($params, '', '&');

 And you should be set.


Good luck.


Thanks for your post on this. I apologize that this caused an issue and took so much of your time to diagnose. After doing a little bit of research, it appears this is not entirely related to the PHP version you are using, but also php.ini configuration options. See http://www.php.net/manual/en/ini.core.php#ini.arg-separator.output for more details.


That beings said, I see no reason to NOT explicitly specify '&' as the separator when using http_build_query, so I will get this change into the library. Thanks again for your efforts!


Update: The following pull request was merged and contains your fix: 


David J

Developer Portal

View API documentation, code samples, get your API key.

Visit Page