I am wanting to use the constant contact REST API to add the ability for users to add them selves to a contact list though a website, as well give them the ability to view previous emails that have been sent out. I have downloaded the PHP samples package, changed the username, password, and api key. But when i deploy this to my server, or try to run it locally I seem to be getting some errors, that I can not figure out. The errors the the same on both environments.
Warning: Invalid argument supplied for foreach() in /home/michaelsvendsen/clients.thesven.com/test/cc_class.php on line 127
Warning: Invalid argument supplied for foreach() in /home/michaelsvendsen/clients.thesven.com/test/cc_class.php on line 149
I have looked though the class, and tried printing out some of the values and to me it seems like the value being returned from the simplexml_load_string method is empty. I am using the same user name and password that I used to create the API key, so I am thinking that these must be correct. Is there something else that must be enabled with the account in order for API access? This is my first time using the constant contact API so any help to point me in the right direction would be greatly appreciated.
Those errors are actually just warnings. The warnings state that the initial time the foreach loop is encountered, the array it is checking to loop through is empty. Once the page is initialized with data, those loops do not encounter the warning.
Warnings are typically displayed in a development environment and while these warnings are not critical and can be left as-is, you will want to adjust your server settings to hide warnings once this script is in place and open to the public.
Support Engineer, Constant Contact
Hey Dan, Michael
1) Php does not produce anything in case if an array is empty. Empty array is an array too. Something else than an array, something that can't be iterated through is used instead.
2) There is nothing good in warnings (and php notices too of course). You should try to avoid any warnings and notices regardless of the environment you use (though you should turn display_errors off in production servers and log all the errors in the file instead of showing them on the page)
3) Probably it's not that bad in this case and can really be left as it is (though you will need to configure your server to hide the errors - already wrong) but all the warning should be taken care of and not just ignored. They may be ignored only in case if you have to use bad code and don't have time to fix it.
I understand your concern with this sample code however we provide this as a sample to assist users in understanding the functionality of our API and how to interact with it using PHP. We have not devoted too much time into this script and as we state on the download page we are providing it as-is and as a learning tool.
I too agree that you should write code that produces no warnings or notices however in order to keep this sample easy to navigate we have not taken the extra step of adding more code to avoid these warnings.
I would also like to clarify and correct my mistake in stating that the warning was caused by an empty array. What I meant was that the array was not yet declared as an array. In the line before the foreach loop the variable $parsedReturn is set to simplexml_load_string($return). Without a value from the simplexml_load_string $parsedReturn is set as an undefined string variable and the foreach loop cannot process.
Support Engineer, Constant Contact
View API documentation, code samples, get your API key.