【问题标题】:Attempt to read property "id" error when to return http response返回 http 响应时尝试读取属性“id”错误
【发布时间】:2021-11-22 05:52:29
【问题描述】:

我尝试从 API 返回,当我尝试返回 API 响应时,会产生此错误:

警告:尝试在第 3 行读取 C:\xampp\htdocs\samuraiacademy\wp-content\themes\twentyseventeen\template-parts\page\content-page.php 中数组的属性“id”

    $vids = [];
    $response = wp_remote_retrieve_body(wp_remote_get(
        'https://developer.dacast.com/v2/vod',
        array(
            'timeout' => 10,
            'headers' => array(
                'X-Format' => 'default'
'X-Api-Key' => '1631861596B5ShPC5xLXu9yjQUqiYfy9KbRUUA6FWr',
            )
        )
    ));

    $result = json_decode($response,true);

    $vids[] = $result;
    foreach($vids[0] as $vid){
        print_r($vid[0]->id);
    }
    
    ?>

如果我 var_dump($vids),

array(1) { [0]=> array(3) { ["data"]=> array(1) { [0]=> array(46) { ["abitrate"]=> int(1000 ) ["acodec"]=> string(3) "aac" ["ads"]=> NULL ["associated_pa​​ckages"]=> string(0) "" ["autoplay"]=> bool(false) ["category_id "]=> int(0) ["container"]=> string(6) "mpeg-4" ["countries_id"]=> string(0) "" ["creation_date"]=> string(20) "2021 -09-27T08:36:48Z" ["custom_data"]=> NULL ["description"]=> string(7) "testing" ["disk_usage"]=> int(123849301) ["duration"]=> string (8) "00:00:01" ["enable_coupon"]=> bool(false) ["enable_payperview"]=> bool(false) ["enable_subscription"]=> bool(false) ["external_video_page"]=> string(7) "http://" ["filename"]=> string(10) "source.mp4" ["filesize"]=> string(8) "83061017" ["google_analytics"]=> int(0 ) ["group_id"]=> int(0) ["hds"]=> string(0) "" ["hls"]=> string(0) "" ["id"]=> string(36) " b2916e15-475e-3527-7b9c-3375bfaa2aa1"

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    不需要将 $result 存储在数组变量中 你得到这样的身份证

     $results = json_decode($response,true);
    
        
        foreach($results as $result){
            print_r($result['id']);
        }
    

    【讨论】:

    • 它产生未定义的数组键“id”错误
    • 我尝试使用您的 var_dump 但数组无效,因此请编辑消息并提供正确的 print_r 数组来解决问题,谢谢您的理解。
    • 我已经把 api 密钥给你测试了
    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 2014-09-06
    • 2013-08-16
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2021-08-19
    相关资源
    最近更新 更多