当涉及到PHP时,我是相当中间的,对我来说,使用API是非常新的。手下留情。
我有一个文件,通过API获取一个绑定器的信息。第二个文件更新绑定器。我要做的是,在更新绑定器时,将从JSON响应ID返回的JSON响应中解析绑定器get_binders.php。
我在get_binders.php文件中尝试了以下操作:
$binder_info = $binder_id->binders->binder[0]->id;
foreach ($binder_info as $binder_id){
echo $binder_id->id;
}当我使用上面添加的代码运行get_binders.php时,我会得到以下内容:
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我尝试过多种不同的东西。我看过很多帖子。我不知道如何获取绑定器ID并将其传递给一个名为$binder_id的变量,以便在运行update_binder.php时调用它。任何帮助都将不胜感激。
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响应
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] =>
)
)
)
)
)更新
使用以下内容更新了get_binders.php:
$binder_info = $response_data['data']['binders'][0]['binder'];
echo $binder_info看看我的代码,我认为$binder_id实际上应该是$response_data。对吗?
当我使用它时,我得到以下信息:
PHP Notice: Array to string conversion in /mnt/e/xampp/htdocs/api_testing/get_binders.php on line 18发布于 2021-05-25 19:50:44
解析的json返回的是数组而不是对象,因此必须将其用作数组。
因此:
$binder_info = $binder_id['data']['binders'][0]['binder']['id'];另外,为什么要在$binder_info上运行foreach循环?因为它是一个字符串,它还会给出一个foreach循环错误。
与foreach循环不同,只需:
echo $binder_info,因为它包含id。
更新
要将变量传递给update_binder页面,只需在get_binder页面中初始化一个新变量$binder_id = $binder_id['data']['binders'][0]['binder']['id'];。
https://stackoverflow.com/questions/67694612
复制相似问题