php 404返回404

qxsslcnc  于 2023-03-28  发布在  PHP
关注(0)|答案(2)|浏览(144)

我的Guzzle POST请求https://api.scarif.dev/auth返回一个404,当页面通过Postman,浏览器或javascript存在时。它应该返回一个200和一个401消息,但Guzzle返回一个404。在POST和GET模式下都是这样。
我尝试了多种客户端设置,包括不同的头文件和禁用SSL验证,但没有任何成功。现在我复制了完全相同的头文件,使其在 Postman 工作,但仍然没有成功。
我一直在google和stackoverflow上搜索,但找不到解决我问题的答案。

PHP请求:

<?php
$client = new Client([
    'header' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/x-www-form-urlencoded'
    ],
    'verify' => false
]);

$response = $client->request('POST', 'https://api.scarif.dev/auth', [
    'form_params' => []
]);

echo $response->getBody()->getContents();
?>

预期成果:

{
    "detail": "https://login.scarif.dev",
    "status": 401,
    "title": "Unauthorized",
    "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html"
}

实际结果:

致命错误:Uncaught GuzzleHttp\Exception\ClientException:客户端错误:POST https://api.scarif.dev/auth导致404 Not Found响应:404 Not Found

Not Found(truncated...)in /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113堆栈跟踪:#0 /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/guzzle/src/Middleware.php(66):Copyright © 2018 - 2019 www.cnjs.com. All Rights Reserved.粤ICP备15048888号-1GuzzleHttp\Middleware::GuzzleHttp{closure}(Object(GuzzleHttp\Psr7\Response))

2/home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/promises/src/Promise.php(156):
GuzzleHttp\Promise\Promise::callHandler(1,Object(GuzzleHttp\Psr7\Response),Array)#3 /home/admin/domains/login.scarif.dev/framework/venin /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/guzzle/src/Exception/RequestException.phpon line 113

API端点控制器:

<?php

namespace Controller;

use Core\Config;
use Core\Request;
use Core\Response;
use Model\Token;
use Model\User;
use MongoDB\BSON\UTCDateTime;

class AuthController extends Controller
{
    public function view(User $user, Token $token)
    {
        extract(Request::getPostData());

        if (isset($access_token) && !empty($access_token)) {
            $_token = $token->getTokenByToken($access_token);

            if (
                $_token['type'] !== Token::TYPE_ACCESS_TOKEN ||
                $_token['expires_on'] <= new UTCDateTime()
            ) {
                return $this->view->display('json', [
                    'payload' => Response::apiResponse(
                        $this->config->get('url.login'), 401
                    )
                ]);
            }

            $token->delete($_token['_id']);

            $newToken = $token->create(Token::TYPE_ACCESS_TOKEN, $_token['user_id']);

            return $this->view->display('json', [
                'payload' => Response::apiResponse($newToken['token'])
            ]);
        }

        if (!isset($email) || !isset($password) || empty($email) || empty($password)) {
            return $this->view->display('json', [
                'payload' => Response::apiResponse(
                    $this->config->get('url.login'), 401
                )
            ]);
        }

        if (!$user->checkCredentials($email, $password)) {
            return $this->view->display('json', [
                'payload' => Response::apiResponse(
                    "The email address or password you've entered is invalid. Please check your entry and try again.",
                    422
                )
            ]);
        }

        $user = $user->getUserByEmail($email);
        $token = $token->create(Token::TYPE_ACCESS_TOKEN, $user['_id']);

        return $this->view->display('json', [
            'payload' => Response::apiResponse($token['token'])
        ]);
    }
}
ktecyv1j

ktecyv1j1#

看起来问题来自你正在使用的API。当使用不同的URL使用你的代码时,它工作得很好:

$client = new Client([
    'header' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/x-www-form-urlencoded'
    ],
    'verify' => false
]);

$response = $client->request('POST', 'https://jsonplaceholder.typicode.com/posts', [
    'form_params' => []
]);

echo $response->getBody()->getContents();

你能展示一下API端点的代码吗?

r8uurelv

r8uurelv2#

我也遇到了同样的问题,一直在寻找解决方案,直到我来到这里。虽然没有在网上得到任何帮助,其他人的解决方案对我不起作用,但后来我通过广泛的调试解决了自己的问题,我分享了解决方案,希望它可能会在未来帮助其他人。
场景:在我的例子中,我有一个API网关和客户端(在我的例子中是 Postman )向API网关发出请求,而网关反过来又向Laravel 8中使用Guzzle 7的微服务发出请求。我曾经将从客户端接收到的所有头部按原样传递给微服务,这导致了404错误。当我改变了这一点,只将请求中的我自己的头部传递给微服务时,灯亮了404就不见了
这些是Postman的默认头,我按原样传入请求:

{
    "authorization": [
        "Bearer eyJ0eXAiOiJKV1 .."
    ],
    "user-agent": [
        "PostmanRuntime/7.29.0"
    ],
    "accept": [
        "*/*"
    ],
    "postman-token": [
        "ca180f3a-ec65-4212-bd9f-dc294846dc65"
    ],
    "host": [
        "sagateway.com"
    ],
    "accept-encoding": [
        "gzip, deflate, br"
    ],
    "connection": [
        "keep-alive"
    ]
}

我删除了所有的内容,只在header中传递了一个东西:

['Authorization' => "<Key Here>"]

然后它工作得很好,在连续几天的谷歌搜索后,我松了一口气。

相关问题