skip to Main Content

I have tried the this code: $record = $record[‘data’]->Company; but it is always returned as null. I have also tried $record = $record[1]->data->Company; which also results in a null value. I have read so many answers and tried the solutions but nothing works for me.

Some more experience than me please have a look and let me know where i am going wrong.

Thanks in advance

{
    "1": {
        "index": 1,
        "data": {
            "CONTACTID": "3345923000000546002",
            "SMOWNERID": "3345923000000158021",
            "Contact Owner": "Frank Rosa",
            "First Name": "Administrator",
            "Last Name": "Ian",
            "Email": "[email protected]",
            "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"
        }
    }
}

I am getting the json from my function below:

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();

        wp_mail( '[email protected]', 'Post Data', $record );

        $payload = json_decode($record, true);

        $payload = $payload[1]['data']['Company'];

        file_put_contents(plugin_dir_path( __FILE__ ).'record.txt', $payload);

       return $payload;
    }
}

4

Answers


  1. You may try to do something like this:

    <?php
    
    $json = '{
        "1": {
            "index": 1,
            "data": {
                "CONTACTID": "3345923000000546002",
                "SMOWNERID": "3345923000000158021",
                "Contact Owner": "Frank Rosa",
                "First Name": "Administrator",
                "Last Name": "Ian",
                "Email": "[email protected]",
                "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"
            }
        }
    }';
    $payload = json_decode($json, true);
    $error = json_last_error();
    if ($error !== JSON_ERROR_NONE) {
        throw new RuntimeException("JSON decode error: $error");
    }
    var_dump($payload[1]['data']['Company']);
    

    As result you will see:

    string(5) "3ELLC"
    

    In case you don’t need associative array but class, try this:

    $payload = json_decode($json);
    $error = json_last_error();
    if ($error !== JSON_ERROR_NONE) {
        throw new RuntimeException("JSON decode error: $error");
    }
    var_dump($payload->{"1"}->data->Company);
    

    Result will be the same.

    Login or Signup to reply.
    1. You have to decode the record like this.

      $array = json_decode($record, true);

    2. Then you can access the data like this.

      echo $array[‘1’][‘data’][‘Company’];

    Checkout it out here, https://www.tehplayground.com/UpHMPFxfSKdxXBmn

    Have a good day!

    Login or Signup to reply.
  2. Having the json resource in the data.json file

    $json = file_get_contents('/tmp/data.json');
    

    Or in a string

    $json = '{
        "1": {
            "index": 1,
            "data": {
                "CONTACTID": "3345923000000546002",
                "SMOWNERID": "3345923000000158021",
                "Contact Owner": "Frank Rosa",
                "First Name": "Administrator",
                "Last Name": "Ian",
                "Email": "[email protected]",
                "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"
            }
        }
    }';
    

    Try the following

    $record = (array) json_decode($json);
    
    print_r($record[1]->data->Company);
    

    Output

    3ELLC
    
    Login or Signup to reply.
  3. It works for me:

    <?php
    $var = json_decode('{
        "1": {
            "index": 1,
            "data": {
                "CONTACTID": "3345923000000546002",
                "SMOWNERID": "3345923000000158021",
                "Contact Owner": "Frank Rosa",
                "First Name": "Administrator",
                "Last Name": "Ian",
                "Email": "[email protected]",
                "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"
            }
        }
    }');
    
    foreach($var as $item) {
        echo $item->data->Company . '<br>';
    }
    ?>
    

    The problem was in the key “1”, you can’t access it like that $var->1, but you can iterate it in an array.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search