Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.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
Php Guzzle对自己服务器的post请求导致“404未找到”`_Php_Laravel_Post_Guzzle - Fatal编程技术网

Php Guzzle对自己服务器的post请求导致“404未找到”`

Php Guzzle对自己服务器的post请求导致“404未找到”`,php,laravel,post,guzzle,Php,Laravel,Post,Guzzle,我正在尝试登录我的laravel应用程序,该应用程序使用passport并添加了guzzle以发布到我的誓言/令牌路线,这样用户就可以登录,而无需我公开我的密钥 然而,我遇到了一个我似乎无法解决的错误 问题 我用passport更新了我的laravel应用程序,并在本地测试了我的后端,效果很好。在我将laravel应用程序部署到live Server测试环境后,我在尝试登录时开始出错 起初guzzle无法验证我当前live环境的SSL。所以我把验证变成了假的。现在我开始得到一个不同的消息,它表明

我正在尝试登录我的laravel应用程序,该应用程序使用passport并添加了
guzzle
以发布到我的
誓言/令牌
路线,这样用户就可以登录,而无需我公开我的
密钥

然而,我遇到了一个我似乎无法解决的错误

问题

我用passport更新了我的laravel应用程序,并在本地测试了我的后端,效果很好。在我将laravel应用程序部署到live Server测试环境后,我在尝试登录时开始出错

起初guzzle无法验证我当前live环境的SSL。所以我把验证变成了假的。现在我开始得到一个不同的消息,它表明我的url
誓言/token
路线是错误的。这是错误的,因为直接发布到我的“誓言/信物”似乎与邮递员合作

我还使用
php-artisan-route:list

总结:guzzle表示我发布的url不存在,但确实存在

我的获取令牌功能

  public function getToken(& $username, $password)
  {
    $http = new \GuzzleHttp\Client;

    try {
      $response = $http->post(config('services.passport.login_endpoint'), [
          //'verify' => false,
          'verify'  => ( env( 'APP_ENV' ) === 'local' ) ? false : true,
          'form_params' => [
              'grant_type' => 'password',
              'client_id' => config('services.passport.client_id'),
              'client_secret' => config('services.passport.client_secret'),
              'scope' => '',
              'username' => $username,
              'password' => $password,
          ]
      ]);
      return $response->getBody();
    } catch (\GuzzleHttp\Exception\BadResponseException $e) {
        if ($e->getCode() === 400) {
            return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
        } else if ($e->getCode() === 401) {
            return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
        }
        return $e;
    }
  }
响应

GuzzleHttp\Exception\ClientException: Client error: `POST https: //my.domain.com/oauth/token` resulted in a `404 Not Found` response:
期望值

  public function getToken(& $username, $password)
  {
    $http = new \GuzzleHttp\Client;

    try {
      $response = $http->post(config('services.passport.login_endpoint'), [
          //'verify' => false,
          'verify'  => ( env( 'APP_ENV' ) === 'local' ) ? false : true,
          'form_params' => [
              'grant_type' => 'password',
              'client_id' => config('services.passport.client_id'),
              'client_secret' => config('services.passport.client_secret'),
              'scope' => '',
              'username' => $username,
              'password' => $password,
          ]
      ]);
      return $response->getBody();
    } catch (\GuzzleHttp\Exception\BadResponseException $e) {
        if ($e->getCode() === 400) {
            return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
        } else if ($e->getCode() === 401) {
            return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
        }
        return $e;
    }
  }
每当
Guzzle
向我的现有路线发布内容时,它就会从我的laravel应用程序返回响应

添加了调试

*   Trying {my ip adres}...
* TCP_NODELAY set
* Connected to {mydomain} {my ip adres} port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: C=CH; L=Schaffhausen; O=Plesk; CN=Plesk; emailAddress=info@plesk.com
*  start date: Feb  9 14: 54: 36 2020 GMT
*  expire date: Feb  8 14: 54: 36 2021 GMT
*  issuer: C=CH; L=Schaffhausen; O=Plesk; CN=Plesk; emailAddress=info@plesk.com
*  SSL certificate verify result: self signed certificate (18), continuing anyway.
> POST /oauth/token HTTP/1.1
Host: {mydomain}
User-Agent: GuzzleHttp/6.3.3 curl/7.58.0 PHP/7.1.33
Content-Type: application/x-www-form-urlencoded
Content-Length: 144

* upload completely sent off: 144 out of 144 bytes
< HTTP/1.1 404 Not Found
< Server: nginx
< Date: Wed,
01 Apr 2020 17: 31: 26 GMT
< Content-Type: text/html; charset=iso-8859-1
< Content-Length: 266
< Connection: keep-alive
< 
* Connection #0 to host {mydomain} left intact
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<hr>
<address>Apache Server at my.domain.com Port 443</address>
</body></html>

您是否尝试使用基本url在配置的login.endpoint前面加上前缀,例如:

$response = $http->post(config('app.url'). config('services.passport.login_endpoint'), [
      ...
    ]
]);

当我运行
php artisan serve
时,我发现了这个解决方案,我可以调用
'base\u uri'=>“127.0.0.1:8000”
。因此,我将其添加到运行
phpartisan-service
,现在我可以调用自己的服务器


显然,我不能通过静态PHP调用自己的服务器

为什么
/
之间有额外的空间?@AlwaysAnny不知道源代码中没有空间。我还尝试直接添加URL,以及您尝试使用php artisan route:list访问的路由的外观如何?@mrhn将其添加到question@Salman从curl响应来看,您的nginx web服务器似乎已经在响应404页面。nginx correclty是否配置为与您的“主机名”匹配?其他路由是否在同一主机名上工作?您是否为此主机名配置了自定义的
位置
匹配项?感谢您的回复。我试着直接把url放在那里,我得到了同样的错误