0

Im pretty intermediate when it comes to PHP and working with APIs is extremely new to me. Please go easy on me.

I have one file that grabs the information of a binder via API. The second file updates the binder. What I am trying to do is, when updating a binder, the binder ID will be parsed from the JSON response get_binders.php returns.

I tried the following within the get_binders.php file:

$binder_info = $binder_id->binders->binder[0]->id;
foreach ($binder_info as $binder_id){
    echo $binder_id->id;
}

When I run get_binders.php with the above code addition, I get the following:

PHP   2. json_decode($json = ['code' => 'RESPONSE_SUCCESS', 'data' => ['unread_feeds' => 0,
 'binders' => [...]]]) /mnt/e/xampp/htdocs/api_testing/get_binders.php:20 
PHP Notice:  Trying to get property 'binders' of non-object in
 /mnt/e/xampp/htdocs/api_testing/get_binders.php on line 21

I have tried multiple different things. I have looked at numerous posts. I cannot figure out how to grab the binder ID and pass that into a variable called $binder_id so I can call that when update_binder.php is run. Any help would be greatly appreciated.

update_binder

<?php

include('create_access_token.php');
include('auth.inc.php');
include('get_binders.php');

// for debugging
// var_dump($token);

$message_text = "This is a new message";

$data = [
    'text' => $message_text
 ];


// turns on php output buffer for debugging
// ob_start();  
// $out = fopen('php://output', 'w');

$data_json = json_encode($data);
$api_url = ''.$site_url.'/v1/'.$binder_id.'/comments?access_token='.$token['access_token'].'';

$ch = curl_init();

// for debugging
// curl_setopt($ch, CURLOPT_VERBOSE, true);  
// curl_setopt($ch, CURLOPT_STDERR, $out);  


// normal curl options
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$binder_data  = curl_exec($ch);

// closes stream and grabs information from output
// fclose($out);  
// $debug = ob_get_clean();

curl_close($ch);
print_r ($binder_data);

// prints debug information
//print_r ($debug);

?>

get_binders.php

<?php

include('create_access_token.php');
include('auth.inc.php');

// for debugging
// var_dump($token);

$tag_value = "Ticket1";

$api_url = ''.$site_url.'/v1/'.$org_id.'/users/'.$email.'/binders?access_token='.$token['access_token'].'&filter=binder&is_email=true&tags_include='.$tag_value.'';

$json_data = file_get_contents($api_url);
$response_data = json_decode($json_data, true);

?>

JSON response

Array
(
    [code] => RESPONSE_SUCCESS
    [data] => Array
        (
            [unread_feeds] => 0
            [binders] => Array
                (
                    [0] => Array
                        (
                            [category] => 0
                            [binder] => Array
                                (
                                    [id] => BMUGClfm93yHvCS3fghoJrL
                                    [name] => Group chat 6
                                    [created_time] => 1621882378530
                                    [updated_time] => 1621882385043
                                    [total_comments] => 0
                                    [total_members] => 1
                                    [total_pages] => 0
                                    [total_todos] => 0
                                    [total_signatures] => 0
                                    [revision] => 6
                                    [thumbnail_uri] => https://demo.site.com/service/themes/images/default/default_binder_cover.png
                                    [conversation] => 
                                    [restricted] => 
                                    [team] => 
                                    [description] => This is just another binder
                                    [feeds_timestamp] => 1621882378516
                                    [status] => BOARD_MEMBER
                                    [last_feed] => Array
                                        (
                                            [published] => 2021-05-24T18:52:58Z
                                            [actor] => Array
                                                (
                                                    [published] => 2021-05-24T18:52:58Z
                                                    [updated] => 2021-05-24T18:53:05Z
                                                    [objectType] => person
                                                    [displayName] => Display Name
                                                    [id] => UAllUUygLEwGlChp6b99bl4
                                                    [image] => https://demo.site.com/service/themes/images/default/avatar-single-360.png
                                                    [unique_id] => 
                                                    [email] => email@email.com
                                                    [phone_number] => +15555555555
                                                    [user_type] => USER_TYPE_NORMAL
                                                )

                                            [verb] => create
                                            [object] => Array
                                                (
                                                    [published] => 2021-05-24T18:52:58Z
                                                    [updated] => 2021-05-24T18:53:05Z
                                                    [objectType] => binder
                                                    [id] => BMUGClfm93yHvCS3fghoJrL
                                                    [displayName] => Group chat 6
                                                    [image] => https://demo.site.com/service/themes/images/default/default_binder_cover.png
                                                    [url] => https://demo.site.com/BMUGClfm93yHvCS3fghoJrL
                                                    [email] => @demo.site.com
                                                )

                                            [generator] => Array
                                                (
                                                    [id] => BMUGClfm93yHvCS3fghoJrL
                                                )

                                            [id] => 4
                                            [all_reads] => 1
                                        )

                                    [binder_email] => 8a9b47220237d4ad1890cb44969eb9dfe@demo.site.com
                                    [tags] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [name] => Ticket1
                                                    [value] => 456789
                                                )

                                        )

                                    [unread_feeds] => 0
                                    [favorite] => 
                                    [inactive] => 
                                )

                        )

                )

        )

)

UPDATE

Updated get_binders.php with the following:

$binder_info = $response_data['data']['binders'][0]['binder'];
echo $binder_info

Looking at my code, I am thinking that $binder_id should actually be $response_data. Is that correct?

When I do use that I get the following:

 PHP Notice:  Array to string conversion in /mnt/e/xampp/htdocs/api_testing/get_binders.php on line 18
Martin
  • 311
  • 1
  • 2
  • 20
  • As you can see from json response, `binders` is inside `data` object and you're trying to access the `binders` property directly from the response `$binder_info = $binder_id->binders->binder[0]->id;` and it's returning error, so change it to `$binder_info = $binder_id->data->binders->binder[0]->id;` – Haridarshan May 25 '21 at 19:46
  • Just updated my answer. you can try that and I think that will be the final solution. – Albert Logic Einstein May 25 '21 at 20:12

1 Answers1

1

The parsed json is returning an array not an object, so you will have to use it as a array.

so:

$binder_info = $binder_id['data']['binders'][0]['binder']['id'];

and also why are you running a foreach loop on a $binder_info ? as it is a string, it will also give an foreach loop error.

instead of the foreach loop, simply:

echo $binder_info as it contains the id.

UPDATE
to pass the variable to your update_binder page, simply initialize a new variable $binder_id = $binder_id['data']['binders'][0]['binder']['id']; in your get_binder page.