5 php http-status-code-404 guzzle
我对https://api.scarif.dev/auth的 Guzzle POST 请求返回 404,而该页面是通过 Postman、浏览器或 JavaScript 存在的。它应该返回 200 和 401 消息,但 Guzzle 返回 404。在 POST 和 GET 模式下都是如此。
我尝试了多种客户端设置,包括不同的标头和禁用 SSL 验证,但没有成功。现在我已经复制了使其在邮递员中工作的完全相同的标头,但仍然没有成功。
我一直在通过谷歌和 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();
?>
Run Code Online (Sandbox Code Playgroud)
预期结果:
{
"detail": "https://login.scarif.dev",
"status": 401,
"title": "Unauthorized",
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html"
}
Run Code Online (Sandbox Code Playgroud)
实际结果:
致命错误:未捕获的 GuzzleHttp\Exception\ClientException:客户端错误:
POST https://api.scarif.dev/auth导致404 Not Found响应:404 Not Found在 /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): GuzzleHttp\Exception\RequestException::create(对象(GuzzleHttp\Psr7\Request),对象(GuzzleHttp\Psr7\Response) ) #1 /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp{closure}(对象(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/ven 在 /home/admin/domains/login 中。 scarif.dev/framework/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php 第 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'])
]);
}
}
Run Code Online (Sandbox Code Playgroud)
问题似乎出在您正在使用的 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();
Run Code Online (Sandbox Code Playgroud)
您能显示 API 端点的代码吗?
| 归档时间: |
|
| 查看次数: |
9218 次 |
| 最近记录: |