当我在 laravel 中学习 http 客户端时,我在浏览器中收到此错误“ErrorException Trying to access array offset on value of type

Posted

技术标签:

【中文标题】当我在 laravel 中学习 http 客户端时,我在浏览器中收到此错误“ErrorException Trying to access array offset on value of type null”【英文标题】:I am getting this error "ErrorException Trying to access array offset on value of type null " in my browser when I am learning http client in laravel 【发布时间】:2021-07-13 02:42:37 【问题描述】:

这是我的用户控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class UserController extends Controller

    //

    public function index()
        $collection= Http::get("https://reqres.in/api/users?page=1");
        return view('users',['collection'=>$collection['data']]);
    

还有我的 composer.json


    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": 
        "php": "^7.3.1|^8.0",
        "fideloper/proxy": "^4.4",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "^7.0.1",
        "laravel/framework": "^8.12",
        "laravel/tinker": "^2.5"
    ,
    "require-dev": 
        "facade/ignition": "^2.5",
        "fakerphp/faker": "^1.9.1",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.2",
        "nunomaduro/collision": "^5.0",
        "phpunit/phpunit": "^9.3.3"
    ,
    "config": 
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    ,
    "extra": 
        "laravel": 
            "dont-discover": []
        
    ,
    "autoload": 
        "psr-4": 
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        
    ,
    "autoload-dev": 
        "psr-4": 
            "Tests\\": "tests/"
        
    ,
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": 
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    

【问题讨论】:

请格式化并解释您的问题。这是非常难以理解的,即使它的格式正确,问题仍然很模糊。 ***.com/help/how-to-ask 我编辑了,还是不可读? 是的,即使问题的格式正确,您也不是在问问题,也没有包括正确的部分,请阅读如何提问 【参考方案1】:

我假设您想要访问 API 端点。您能否尝试检查您是否在 API 调用中返回任何状态。使用 this$response = Http::get($url); $response-&gt;status() 来检查响应的状态。如果它工作,你可以尝试获取响应的 json。

【讨论】:

以上是关于当我在 laravel 中学习 http 客户端时,我在浏览器中收到此错误“ErrorException Trying to access array offset on value of type 的主要内容,如果未能解决你的问题,请参考以下文章

从Laravel,Yii,Thinkphp中学习php 操作数据库的事务嵌套

PHP精讲2021—如何获取真实客户端IP方法中学习扩展思维

PHP精讲2021—如何获取真实客户端IP方法中学习扩展思维

在颤振中发出 http.get() 请求的问题

如何将 laravel 版本从 8.x 降级到 7.x?

设计模式15--从审批流中学习责任链模式