PHP json_decode fails when double-quote in contact

edited August 2015 in Social Sharing
Hello,

I'm using the PHP User API \ read_user_contacts and notice that PHP json_decode(...) fails when a contact (in my case from backend Google/Google+ contacts) when e.g. the name - .............['name']['formatted'], contains a double quote, e.g. like:

John "Golf" Johnson

I wonder if these double quotes preferably should have been removed on the oneall side? It is somehow hard to parse and remove it after receiving the data from oneall and before invoking PHP json_decode(...).

Best regards,

Kevin Kliland

Best Answer

Answers

  • Hi,

    Could you please post to us your part of your code that produces the json_decode error? (here, or by email if you prefer).

    Thanks!
  • edited August 2015

    will come back soon.....

  • Hi,

    Thanks for coming back to me.

    The (-test) code is as follows:

    //LIST ALL CONTACTS FOR EACH USER TOKEN
    $oneall_curly->get (SITE_DOMAIN . "/users/" . $user_token . "/contacts.json?disable_cache=true");
    $result_inner = $oneall_curly->get_result ();

    if ($result_inner->http_code == 200)
    {
    $body_inner = $result_inner->body;
    $body_inner_new = json_decode($body_inner, true); <---- NOTE: FAILS, IS EMPTY
    if (empty($body_inner_new)) <---- NOTE: ENTERS HERE
    {
    echo 'JSON LAST ERROR ' . json_last_error() . "\n";
    echo 'JSON ERROR MSG ' . json_last_error_msg() . "\n";
    }
    else
    {
    ...........
    }

    It is easy to reproduce, when I remove the double quotes inside the name in my contacts (e.g. from Google) everything works fine. When I add a double quote (") to one of my contacts the problems comes back again.

    E.g.:

    John "Golf" Johnson is not ok.

    John Johnson is ok.

    John Golf Johnson is ok.

    I've tried json_decode with and without giving 'true' as parameter but with the same result (i.e. in order on working on PHP arrays or objects).

    I was first able to find out what the problem actually was after installing jsonlint on Ubuntu for parsing the JSON, the program told me which strings was incorrect JSON (e.g. "Golf" in my example above).
  • Hi Claude,

    Yes, that is correct I'm using the PHP SDK.

    Thanks so much for so swift and extremely good support.

    Yes, your suggestion worked just perfect :-)

    Regards,

    Kevin
  • Hi,
    The correction has been pushed to the github repository (https://github.com/oneall/php-sdk).
    Regards.
  • Engaging in cricket involves a mix of physical activities such as running, sprinting, throwing, and catching. This helps players improve their cardiovascular fitness, agility, coordination, and overall strength psl schedule, contributing to enhanced physical well-being.

Welcome!

Please sign in to your OneAll account to ask a new question or to contribute to the discussions.

Please click on the link below to connect to the forum with your OneAll account.