Brightpearl API Error ( CMNC-009 )
Ok so after having a couple of debug issues with a PHP script that was calling on the Brightpearl API, one of our guys was continually getting :
[code] => CMNC-009
[message] => The request body could not be read while attempting to parse it as JSON
Anyhow after overlooking something really obvious for far too long, it was realised that it was being caused by double encoding json encoding.
So the postdata was being encoded then sent to a controller which was encoding the postdata into json !
Anyhow a real simple one, but something that was easy to over look, hopefully this will save somebody some time if they get the same issue, just dump your postdata json data just before it is used in your API call and check it matches the required structure.
Code Snippet: Getting first character or digit of a domain in PHP
After a very minor scuffle at Mad Capsule over the best way to get a the first letter or digit of a domain name from a variable, I thought I would post my solution online. Usually it would not occur to me to post such a short PHP snippet however I've been informed that the solution was not as easy to find online as it should have been for our non-native PHP developer.
The argument was if it was better to use regex or a combination of built in PHP functions. Essentially we are just trying to get the first character from a string in PHP however we need to take into account the occurrence of HTTP(s) and any www's. The code I wrote to do this, that I champion over a complex regular expression, is below.
PHP:
$domain = "http://www.testdomain.com"; | |
echo substr(str_replace(array('http://','https://','www.'), '', $domain), 0,1); | |
// outputs ' t ' |
If you have a better or alternative way of doing this, please post it below.
Selecting data from MySql using a PHP array containing strings

Just a quick note on selecting mysql data using PHP arrays, specifically arrays containing strings.
MySql has the ability to accept array values via IN comparison operator.
See below:
SELECT * FROM table_name WHERE colum1 IN ($array)
Which could equate to something like:
SELECT * FROM table_name WHERE colum1 IN (2, 3, 8)
This will select all data from table_name where colum1 = all array values*
*Assuming these values are numerical and not strings.
If you want to select MySql data from an array that contains strings then one of the quickest and easiest possibilities is to implode your array into a single string to form a new variable that you can use in your MySql SELECT command in brackets after IN.
Like so:
$array_strings = implode("', '", $array();
SELECT * FROM table_name WHERE colum1 IN ('$array_strings')
Here we are using $array_strings to store all values inside of $array() separated with what ever is between ""
For instance:
$array[] = "ax";
$array[] = "au";
$array[] = "xb";
$array_strings = implode("|", $array();
echo $array_strings
Would display:
ax|au|xb
A good thing to know, but not exactly fitting for the syntax required while using IN.
We need to read IN (ax, au, xb).
So we added ' ' to either side of our variable $array_strings and place all of that in brackets:
$array[] = "ax";
$array[] = "au";
$array[] = "xb";
$array_strings = implode("' '", $array();
$sql="SELECT * FROM table_name WHERE colum1 IN ('$array_strings')";
Hopefully somebody will find this of some use
Notice: Use of undefined constant MCRYPT_BLOWFISH
Of late there have been a noticeable amount of support requests on freelance websites and on the Magento forums regarding the MCRYPT_BLOWFISH error some users have seenon both Linux and Microsoft servers. Some are effected after server moves and others suggest it is random. Here we are going to take a quick look at the error, why it has happened and how to resolve it.
The reason for the error is actually quite simple and the fix is usually not a massive problem either unless your server is running old or legacy software. The error is caused when PHP cannot find the php-mcrypt module and or the libmcrypt libary.
How to install php-mcrypt and libmcrypt on my Linux server?
First find out which version of PHP you are running and also find out your version of Linux. Once you know your version of Linux you need to lookup how to install programs on there. Usually this will be done with the RPM command, apt-get command or yum install command etc. Then at the command line type (without the hash and replacing apt-get with whatever you installing is, of course) ...
# apt-get install php5-mcrypt
or if you are running php 4 ..
# apt-get install php4-mcrypt
Also if you don't have the library we mentioned ...
# apt-get install libmcrypt
Once you have installed those you will need to restart apache to get them to work. That should be it! If you are having troubles with this then post a comment below and let us know your system specs, we will do our best to help.
How to install php-mcrypt and libmcrypt on my Windows server?
Download libmcrypt.dll from php.net and place it in your PHP directory on your server. This is the directory that usually holds the PHP configuration files and so forth. Now open up php.ini (usually in the same directory, if not then do a Windows search for it) and find the line...
# extension=php_mcrypt.dll
Remove the hash '#' and restart your server and you should be good to go.
Debugging Cake PHP Applications
A common question new starters to CakePHP ask is regarding debugging. Many users start off with basic scaffolding and confronted with the uninformative error ....
Not Found
The requested URL /index.php was not found on this server.
This is because your CakePHP application is set to production mode. In production mode you would not want your visitors and clients seeing detailed error reports, it would worry them.
To find out what the actual problem is open up file app/config/core.php and find line 45(ish) and you will see the following ....
Configure::write('debug', 0);
Change the above value from 0 to either 1, 2 or 3 and you will get a much more helpful error message. We recommend you set it to 2.
Direct from the CakePHP comments itself; here is an explanation of each debug level...
* Development Mode:
* 1: Errors and warnings shown, model caches refreshed, flash messages halted.
* 2: As in 1, but also with full debug messages and SQL output.
* 3: As in 2, but also with full controller dump.
It is worth noting that when debug level is set to anything other than production mode (0), any 'flash' messages and redirects you have will not redirect automatically, you must click the link to forward on.
:: Next >>