GuzzleHTTP在现有页面上返回404

问题描述 投票:0回答:1

我向Guzxswpoi发出的Guzzle POST请求返回404,而该页面通过Postman,浏览器或javascript存在。它应该返回一个带有401消息的200,但Guzzle返回404.在POST和GET模式下都是。

我尝试过多个客户端设置,包括不同的标头和禁用SSL验证,但没有任何成功。现在我复制了完全相同的标题,使其在邮递员中工作,但仍然没有成功。

我一直在搜索谷歌和stackoverflow,但找不到解决我的问题的答案。

请求PHP:

https://api.scarif.dev/auth

预期结果:

<?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();
?>

实际结果:

致命错误:未捕获GuzzleHttp \ Exception \ ClientException:客户端错误:{ "detail": "https://login.scarif.dev", "status": 401, "title": "Unauthorized", "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html" } 导致POST https://api.scarif.dev/auth响应:404 Not Found

Not Found (truncated...) in /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 Stack trace: #0 /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/guzzle/src/Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\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)/ home / admin / domains / login中的#3 /home/admin/domains/login.scarif.dev/framework/ven。第113行的scarif.dev/framework/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php

API端点控制器:

404 Not
  Found
php http-status-code-404 guzzle
1个回答
0
投票

似乎问题来自您正在使用的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'])
        ]);
    }
}

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

© www.soinside.com 2019 - 2024. All rights reserved.