账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Laravel5.5的Testing无法请求远程地址问题
    18
    0

    使用Laravel5.5的Testing做api测试的时候,发现无论怎么设置请求的都是本地的url。查看源码发现

    vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php

    文件下有段代码

    /**
     * Call the given URI and return the Response.
     *
     * @param  string  $method
     * @param  string  $uri
     * @param  array  $parameters
     * @param  array  $cookies
     * @param  array  $files
     * @param  array  $server
     * @param  string  $content
     * @return \Illuminate\Foundation\Testing\TestResponse
     */
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
    {
        $kernel = $this->app->make(HttpKernel::class);
    
        $files = array_merge($files, $this->extractFilesFromDataArray($parameters));
    
        $symfonyRequest = SymfonyRequest::create(
            $this->prepareUrlForRequest($uri), $method, $parameters,
            $cookies, $files, array_replace($this->serverVariables, $server), $content
        );
    
        $response = $kernel->handle(
            $request = Request::createFromBase($symfonyRequest)
        );
    
        if ($this->followRedirects) {
            $response = $this->followRedirects($response);
        }
    
        $kernel->terminate($request, $response);
    
        return $this->createTestResponse($response);
    }

    其中

    $response = $kernel->handle(
            $request = Request::createFromBase($symfonyRequest)
        );

    我猜测laravel根本没有发送远程请求,而是直接构造$requset灌入kernel执行,没有发起http请求就意味着没有经过nginx,apache之类的web服务器。这样的测试跟实际生产环境有相当差异。

    PS: PHP发起http请求几种方式

    file_get_contents
    curl
    fsocket
    GuzzleHttp

    没有查到$kernel->handle($request);之后有执行远程请求的

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 斑点熊猫 普通会员 1楼

      在 Laravel 5.5 中,如果你尝试请求远程地址,但是遇到无法请求的问题,那么可能有以下几种情况:

      1. 你的网络连接可能有问题。你可以尝试ping 你的服务器 IP 或者访问一些测试网站来检查你的网络连接。

      2. 你的服务器可能没有配置好。你可以检查你的服务器配置文件,确保 url 选项设置正确。

      3. 你的数据库连接可能有问题。你可以尝试使用 闷头数据库 或者使用 dd 命令来查看数据库连接信息。

      4. 你的路由可能没有配置正确。你可以检查你的路由配置,确保路由正确。

      5. 你的中间件可能没有配置正确。你可以检查你的中间件配置,确保中间件正确。

      如果你无法确定问题的原因,建议你检查 Laravel 的文档或者在网上搜索相关的解决方案。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部