0

I am using the API here: http://developers.livechatinc.com/rest-api/#!chats

I am trying to parse data being returned through my LiveChat service provider. Here is what is returned after a successful API call:

object(stdClass)#3 (3) {
  ["chats"]=>
  array(1) {
    [0]=>
    object(stdClass)#4 (17) {
      ["type"]=>
      string(4) "chat"
      ["id"]=>
      string(10) "MUQM3RLYBV"
      ["visitor_id"]=>
      string(22) "S1381706997.d8b6736611"
      ["agents"]=>
      array(1) {
        [0]=>
        object(stdClass)#5 (2) {
          ["display_name"]=>
          string(17) "Display Name"
          ["email"]=>
          string(30) "Email"
        }
      }
      ["supervisors"]=>
      array(0) {
      }
      ["rate"]=>
      string(10) "rated_good"
      ["duration"]=>
      int(50)
      ["group"]=>
      array(1) {
        [0]=>
        int(1)
      }
      ["started"]=>
      string(25) "Sun, 10/13/13 06:30:39 pm"
      ["pending"]=>
      bool(false)
      ["tags"]=>
      array(0) {
      }
      ["prechat_survey"]=>
      array(3) {
        [0]=>
        object(stdClass)#10 (3) {
          ["key"]=>
          string(5) "Name:"
          ["value"]=>
          string(12) "Demo Name"
          ["id"]=>
          string(18) "138133839826202879"
        }
        [1]=>
        object(stdClass)#11 (3) {
          ["key"]=>
          string(7) "E-mail:"
          ["value"]=>
          string(14) "[email protected]"
          ["id"]=>
          string(18) "138133839826305342"
        }
        [2]=>
        object(stdClass)#12 (3) {
          ["key"]=>
          string(17) "Choose a Subject:"
          ["value"]=>
          string(4) "Math"
          ["id"]=>
          string(18) "138133839826304607"
        }
      }
      ["started_timestamp"]=>
      int(1381707039)
      ["ended_timestamp"]=>
      int(1381707089)
      ["ended"]=>
      string(25) "Sun, 10/13/13 06:31:29 pm"
    }
  }
  ["total"]=>
  int(1)
  ["pages"]=>
  int(1)
}

I am having a really hard time grabbing certain pieces of data I need to obtain from the returned data. Here is the code I have been using but it does not work:

<?php
require_once('lib/LiveChat_API.php');

try {
    $LiveChatAPI = new LiveChat_API();
    $output = $LiveChatAPI->chats->get();
    $array = json_decode($output, true); //Saves the returned JSON object as a multi-dimensional array

    echo $array['prechat_survey']['E-mail'];

} catch (Exception $e) {
    die($e->getCode().' '.$e->getMessage());
}
?>

For example, How would I get the value of "E-mail" under the "prechat_survey"?

1 Answer 1

1

Would this work?

$array['prechat_survey'][1]['value'];

Update

Looks like you are already getting an object, not a JSON string...

Try and see if this works:

try {
    $LiveChatAPI = new LiveChat_API();
    $output = $LiveChatAPI->chats->get();

    echo $output->prechat_survey[1]->value;

} catch (Exception $e) {
    die($e->getCode().' '.$e->getMessage());
}

Upadte2 I see, there's a chats object I din't see

Try this:

echo $output->chats[0]->prechat_survey[1]->value;
Sign up to request clarification or add additional context in comments.

6 Comments

I am getting an error that says "Warning: json_decode() expects parameter 1 to be string, object given". What needs to be changed in my code? Thanks!
Looks like it's an object already. Try the update (give me 2 seconds to update the answer)
Okay, I am not getting any more errors which is good! When I tried printing out $output->prechat_survey[1]->value; it is blank. I think you are close though
try doing a var_dump($output) and paste the results?
I just did that and it prints out exactly what I have in my original posting.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.