我对https://api.scarif.dev/auth的POST请求返回了404,而该页面通过Postman,浏览器或javascript存在。它应该返回200并显示401消息,但Guzzle会返回404。在POST和GET模式下,都是这样。
我尝试了多种客户端设置,包括不同的标头和禁用SSL验证,但没有成功。现在,我复制了与在邮递员中可用的完全相同的标头,但仍然没有成功。
我一直在搜索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"
}
实际结果:
致命错误:未捕获的GuzzleHttp \ Exception \ ClientException:客户端 错误:
POST https://api.scarif.dev/auth
导致404 Not Found
响应: 找不到404(被截断...)在 /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(Object(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}(Object(GuzzleHttp \ Psr7 \ Response))
2 /home/admin/domains/login.scarif.dev/framework/vendor/guzzlehttp/promises/src/Promise.php(156):
GuzzleHttp \ Promise \ Promise :: callHandler(1, 对象(GuzzleHttp \ Psr7 \ Response),数组)#3 /home/admin/domains/login.scarif.dev/framework/ven in /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'])
]);
}
}
答案 0 :(得分:0)
问题似乎出在您使用的API上。将代码与其他网址一起使用时,效果很好:
$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端点的代码吗?