我在使用GuzzleHttp客户端服务的Laravel 5.4
应用程序遇到问题时,我尝试访问网址以获取登录令牌然后获取登录信息,在邮递员的尝试中,我能够得到类似这样的数据:
然后我提取这样的登录信息:
现在用我的guzzle http客户端获取相同内容时,我使用一般控制器获取要传递的URL的信息,并在我的控制器中发送请求参数,我有一些东西像这样:
public function __construct(GuzzleHttp\Client $client)
{
$this->client = $client;
}
public function getRequest( Request $request)
{
$request = $this->client->post($request->url, [
'headers' => ['Content-Type' => 'application/json'],
'body' => json_encode($request->request_data)
]);
$response = \GuzzleHttp\json_decode($request->getBody());
return response()->json(['data' => json_decode($response->d)],$request->getStatusCode());
}
现在,虽然访问我的结果不正确:
首先是登录网址:
然后登录信息:
我收到了未经授权的错误:
我不知道该怎么做,即使拨打下一个网址也几乎没有时间,我已经检查了邮递员没有使用过的cookie或者没有涉及会话数据。不知道该怎么做,请帮我解决这个问题。
答案 0 :(得分:0)
在此行twig
中更改$ request变量名,从函数参数覆盖$ request变量。再次测试。
您的代码应该是这样的
$request = $this->client->post($request->url, [