iContact php jSON parsing issue

96 Views Asked by At

I am playing around with iContact API and am running into an issue. After creating a contact using the api I am attempting to retrieve the most recent response so I can parse the contactId in order to send that contact a message or add them to a list.

// adds new client to iContact
var_dump($oiContact->addContact('[email protected]', null, null, 'Joe', 'Smith', null, '123 Somewhere Ln', 'Apt 12', 'Somewhere', 'NW', '12345', '123-456-7890', '123-456-7890', null));

// Gets last response (results are showing below
$obj = $oiContact->getLastResponse();

When print the $obj var this is what I get

{"contacts":[{"contactId":"1009090","prefix":"","firstName":"Joe","lastName":"Smith","suffix":"","street":"123 Somewhere Ln","street2":"Apt 12","city":"Somewhere","state":"NW","postalCode":"12345","phone":"123-456-7890","fax":"123-456-7890","business":"","email":"[email protected]","createDate":"2014-04-24 01:31:59","bounceCount":"","status":"normal"}]}

    //decode json object and echo it
    $data = json_decode($obj,TRUE);
    echo $data->contacts[0]->contactId;

Error Message: Notice: Trying to get property of non-object in …(path)... on line 24

I looked on other posts and attempted to duplicate some of the same solutions but nothing seemed to work. Thanks for any help in advanced.

1

There are 1 best solutions below

0
On BEST ANSWER

If you are going to access in object method remove the TRUE from json_decode function parameter list. If you pass TRUE it returns an array.

$data = json_decode($obj);

Otherwise you have to access like this.

//decode json object and echo it
$data = json_decode($obj,TRUE);
echo $data['contacts'][0]['contactId'];