Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将响应中嵌套的JSON数据解析为PHP变量_Php_Json - Fatal编程技术网

将响应中嵌套的JSON数据解析为PHP变量

将响应中嵌套的JSON数据解析为PHP变量,php,json,Php,Json,说到PHP,我是相当中级的,使用API对我来说是非常陌生的。请对我宽容点 我有一个通过API获取绑定器信息的文件。第二个文件更新活页夹。我试图做的是,在更新活页夹时,活页夹ID将从JSON响应get\u binders.php返回解析 我在get_binders.php文件中尝试了以下操作: $binder_info = $binder_id->binders->binder[0]->id; foreach ($binder_info as $binder_id){ e

说到PHP,我是相当中级的,使用API对我来说是非常陌生的。请对我宽容点

我有一个通过API获取绑定器信息的文件。第二个文件更新活页夹。我试图做的是,在更新活页夹时,活页夹
ID
将从JSON响应
get\u 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
$binder_info = $response_data['data']['binders'][0]['binder'];
echo $binder_info
 PHP Notice:  Array to string conversion in /mnt/e/xampp/htdocs/api_testing/get_binders.php on line 18
我试过多种不同的方法。我看过很多帖子。我不知道如何获取活页夹
ID
并将其传递到名为
$binder\u ID
的变量中,以便在运行
update\u binder.php
时调用该变量。任何帮助都将不胜感激

更新活页夹

<?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   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
$binder_info = $response_data['data']['binders'][0]['binder'];
echo $binder_info
 PHP Notice:  Array to string conversion in /mnt/e/xampp/htdocs/api_testing/get_binders.php on line 18
看看我的代码,我认为$binder\u id实际上应该是$response\u数据。对吗

当我使用它时,我得到以下信息:

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
$binder_info = $response_data['data']['binders'][0]['binder'];
echo $binder_info
 PHP Notice:  Array to string conversion in /mnt/e/xampp/htdocs/api_testing/get_binders.php on line 18

解析后的json返回的是一个数组而不是一个对象,因此您必须将其用作数组

因此:

还有,为什么要在
$binder\u info
上运行foreach循环?因为它是一个字符串,所以它也会给出一个foreach循环错误

而不是foreach循环, 简单地说:

echo$binder\u info
,因为它包含id

更新

要将变量传递到更新活页夹页面,只需初始化一个新变量
$binder\u id=$binder\u id['data']['binders'][0]['binder']['id']在您的get_binder页面中。

正如您从json响应中看到的,
binders
位于
data
对象中,您试图直接从响应
$binder_info=$binder_id->binders->binder[0]->id访问
binders
属性返回错误,因此将其更改为
$binder\u info=$binder\u id->data->binders->binder[0]->id刚刚更新了我的答案。你可以试一试,我认为这将是最终的解决方案。更新了我的原始帖子你太棒了。运行时没有出错。添加了var_转储($binder_info);事实上,我得到了活页夹的
ID
。非常感谢你@Martin Welcome检查并运行了我的update_binders.php文件,它用一条新消息更新了绑定器。@Martin消息是什么?