BrightcoveOS / PHP-MAPI-Wrapper

This project provides a starting point for integrating the Brightcove Media API into your application. It provides simple ways to interact with the API, as well as a long list of helper functions.
http://opensource.brightcove.com/project/PHP-MAPI-Wrapper/
48 stars 51 forks source link

Unknown JSON decoding error -- adding MRSS output method #5

Closed mshmsh5000 closed 13 years ago

mshmsh5000 commented 13 years ago

One implementation of MAPI calls using the PHP class is failing (PHP 5.2.14) because of a JSON decoding error. Because json_last_error() isn't available in PHP < 5.3.0, I can't debug.

I am implementing the MRSS option in the PHP-MAPI-Wrapper because that's the only way I see around this.

mcongrove commented 13 years ago

Can you provide more information on where exactly it's failing, and what you're trying to do when it does?

mshmsh5000 commented 13 years ago
$bc = new BCMAPI($bc_api_key);
$videos_all = $bc->find('allvideos');

In BCMAPI::getData():

$response_object = json_decode(preg_replace('/[[:cntrl:]]/', '', $response));

renders an empty object. No error messages are available. My updated code in this method:

$response_object = json_decode(preg_replace('/[[:cntrl:]]/', '', $response));

if (empty($response_object) && function_exists('json_last_error')) {
  $json_errors = array(
    JSON_ERROR_NONE => 'No error has occurred',
    JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
    JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
    JSON_ERROR_SYNTAX => 'Syntax error',
  );
  echo 'JSON error: ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
  exit;
}
else {
  echo 'JSON error, but can\'t debug, because your version of PHP is too old.';
  exit;
}

With this, the result of my calling

$videos_all = $bc->find('allvideos');

is JSON error, but can't debug, because your version of PHP is too old.

I'm definitely receiving the full JSON response from BC -- I've verified that, and as I said, when I request MRSS I can parse the results without a problem.

mcongrove commented 13 years ago

That's certainly odd. I've tested on my PHP 5.2 implementation and all is fine.

Can you do me a favor and try the following?

echo "Raw data:\n";
print_r($response);
echo "<hr />";

$response_object = json_decode(preg_replace('/[[:cntrl:]]/', '', $response));

echo "Original:\n";
print_r($response_object);
echo "<hr />";

$response_object = json_decode($response);

echo "No Regex:\n";
print_r($response_object);
mshmsh5000 commented 13 years ago

Interesting -- looks like the regex.

$response returns the raw response, as expected. "Original" is blank. "No Regex" is a proper decoded response:


Original:
No Regex: stdClass Object ( [items] => Array ( [0] => stdClass Object ( [id] =>...

So, may not be a JSON issue at all, but a preg_replace issue. When I do run the preg_replace(), preg_last_error() returns 0. There may be a character set problem. For instance, I see this in the response:

Whatever your hair type, we’ve got the lowdown on the best brush for you

Indeed, changing the preg_replace line to

$response_object = json_decode(preg_replace('/[[:cntrl:]]/u', '', $response));

works fine. I think that's the fix for this case.

mcongrove commented 13 years ago

Awesome, thanks for that information. It's an odd issue (I don't see how the regex would clear the data entirely), but we'll look into a solution.

In the meantime, just kill the regex in your version of the code and you should be good to go.

Thanks again!

mshmsh5000 commented 13 years ago

Should have been more clear -- keep the regex, just pass the /u flag at the end of the expression. This works fine, because the root cause appears to be a regex error on char data coming from Brightcove.

http://us2.php.net/manual/en/reference.pcre.pattern.modifiers.php

mcongrove commented 13 years ago

Ah, sorry, didn't notice the new modifier. Sounds great! I'll try and get a new release out tonight with the /u modifier.

Thanks once more for helping debug and solve the issue. Much appreciated!

mcongrove commented 13 years ago

Actually, went ahead and updated it. Source code is fixed.