0

i have an issue where when i do a return $record in my function below i get valid json in postman pretty view but if i try print_r($record) i have a null at the end of the json.

Then when i try to json_decode the out put is always null for some reason.

If anyone has any ideas why this might be happening i am greatful for your input.

Thanks

function webhook_listener($request_data){
        $client = new ZohoCRMClient('Contacts', 'API key Here');

        $parameters = $request_data->get_params();

        if( !isset( $parameters['contactId'] ) || empty($parameters['contactId']) ){
            file_put_contents(plugin_dir_path( __FILE__ ).'invalid.txt', 'No parameter found');
            return array( 'error' => 'no_parameter_given' );
        }else{
            $companyid = $parameters['contactId'];

            //file_put_contents(plugin_dir_path( __FILE__ ).'crm.txt', $parameters);

            $record = $client->getRecordById()->id($companyid)->request();

            $record = json_decode($record);

            $error = json_last_error();
            if ($error !== JSON_ERROR_NONE) {
                throw new RuntimeException("JSON decode error: $error");
            }

            echo $company = $record[1]['data']['Company'];

        }

    }

JSON:

{"1":{"index":1,"data":{"CONTACTID":"3345923000000546002","SMOWNERID":"3345923000000158021","Contact Owner":"Frank Rosa","First Name":"Administrator","Last Name":"Ian","Email":"poojarajan3ellc@gmail.com","Created Time":"2018-09-19 14:32:35","Modified Time":"2018-09-20 02:48:51","Full Name":"Administrator Ian","Description":"Equity and Empowerment through Education. It is the Mission of 3e LLC to promote equity and empowerment for all students through engaging professional development for educators and parents, one-on-one coaching for teacher efficacy, and mentoring services for youth to promote enrichment and success. For the empowered, we offer editing, transcribing, and ghostwriting services to ensure your voice is heard.","Last Activity Time":"2018-09-20 02:48:51","Instagram Url":"http:\/\/www.instagram.com\/3e_llc","Company":"3ELLC","Website":"https:\/\/www.3ellc.org","Phone_1":"(727) 420-1050","Full Address":"2152 Arcadia Rd, Holiday, FL 34690, USA","Facebook Url":"http:\/\/www.facebook.com\/3eLLC\/","Logo Url":"https:\/\/dev.energypages.com\/wp-content\/uploads\/2018\/05\/header-logo-57.png","Twitter Url":"http:\/\/www.twitter.com\/3e_llc","Membership Level":"Basic","Select Service":"Technology","User ID":"347"}}}

var_dump output:

array(1) {
  [1]=>
  object(CristianPontes\ZohoCRMClient\Response\Record)#2068 (2) {
    ["index"]=>
    int(1)
    ["data"]=>
    array(22) {
      ["CONTACTID"]=>
      string(19) "3345923000000546002"
      ["SMOWNERID"]=>
      string(19) "3345923000000158021"
      ["Contact Owner"]=>
      string(10) "Frank Rosa"
      ["First Name"]=>
      string(13) "Administrator"
      ["Last Name"]=>
      string(3) "Ian"
      ["Email"]=>
      string(25) "poojarajan3ellc@gmail.com"
      ["Created Time"]=>
      string(19) "2018-09-19 14:32:35"
      ["Modified Time"]=>
      string(19) "2018-09-20 02:48:51"
      ["Full Name"]=>
      string(17) "Administrator Ian"
      ["Description"]=>
      string(407) "Equity and Empowerment through Education. It is the Mission of 3e LLC to promote equity and empowerment for all students through engaging professional development for educators and parents, one-on-one coaching for teacher efficacy, and mentoring services for youth to promote enrichment and success. For the empowered, we offer editing, transcribing, and ghostwriting services to ensure your voice is heard."
      ["Last Activity Time"]=>
      string(19) "2018-09-20 02:48:51"
      ["Instagram Url"]=>
      string(31) "http://www.instagram.com/3e_llc"
      ["Company"]=>
      string(5) "3ELLC"
      ["Website"]=>
      string(21) "https://www.3ellc.org"
      ["Phone_1"]=>
      string(14) "(727) 420-1050"
      ["Full Address"]=>
      string(39) "2152 Arcadia Rd, Holiday, FL 34690, USA"
      ["Facebook Url"]=>
      string(30) "http://www.facebook.com/3eLLC/"
      ["Logo Url"]=>
      string(73) "https://dev.energypages.com/wp-content/uploads/2018/05/header-logo-57.png"
      ["Twitter Url"]=>
      string(29) "http://www.twitter.com/3e_llc"
      ["Membership Level"]=>
      string(5) "Basic"
      ["Select Service"]=>
      string(10) "Technology"
      ["User ID"]=>
      string(3) "347"
    }
  }
}
null

Output above is the raw view in postman. and in pretty view i got this error (Unexpected 'a')

Dian Reid
  • 41
  • 1
  • 8

1 Answers1

0

All the answers here on how to parse jSON were correct. But i was using a php library for my link to zoho which handles the data differently, so i had to do a foreach on my response from the API then json_encode() the data and json_decode() to get the values.

Library i am using: https://github.com/cristianpontes/zoho-crm-client-php

foreach($record as $records){
            $payload = $records->getData();
            $payload = json_encode($payload);
            $payload = json_decode($payload, true);

            $error = json_last_error();
            if ($error !== JSON_ERROR_NONE) {
                throw new RuntimeException("JSON decode error: $error");
            }

            $company = $payload['Company'];
}

Thanks to everyone that helped especially @miken32

Dian Reid
  • 41
  • 1
  • 8