The test would be to dump the following after the curl_exec
:
[ 'uri' => $accessURI,
'httpcode' => curl_getinfo ( $curl, CURLINFO_HTTP_CODE ),
'profile' => $profile ]
The httpcode
to check if the request was valid - could be that:
- Company number was invalid - note that company numbers have to be 8 characters, left-padded with ‘0’ if less, and letters in uppercase.
- Company number didn’t exist in the CH API dataset. Not all companies do (see posts about missing ones / when CH remove ceased companies etc.)
Another possible is that since the function json_decode
returns null if it can’t parse the input it could be the decoding. It could be the json returned is incorrect JSON - or at any rate JSON that your PHP’s version of json_decode
can’t handle (I’m saying the last because I’ve encountered too many odd behaviours in PHP…BTW which version are you running?).
The errors seem to indicate (if they’re always the same) that json_decode is indeed returning null here (try running ‘null
’ and then ‘{ }
’ through your code e.g. putting it into json_decode
to compare).
See the PHP manual page on the json_last_error()
function and particularly the json_last_error_msg()
function in case you need to check if these are giving errors (in the comments there’s a script to provide a version if you’re at PHP < 5.5). You could do:
// cast to string just in case, use @ since message can be raised
$profileObj = @json_decode( (string) $profile );
if ( JSON_ERROR_NONE !== json_last_error() ) {
// throw an error or return 'JSON error: ' . json_last_error_msg();
...
A way to simplify a lot of this is using an http library e.g. Guzzle. (Caveat - we don’t on this project but may move to it).
Of course in an ideal world, CH would publish their Swagger spec / OpenAPI spec for the API and you’d be able to run Swagger Codegen to create all the code stubs to handle the basic communication. (In my experience of these it is never that simple of course…)