我使用GuzzleHttp发送请求,外部API,并得到响应,但返回的响应是从数据空。当我在测试其他高级客户端,我得到一个数据的URI和参数,那么,为什么狂饮响应是空的?如果可以,请你帮助我。
这里是我的代码:
public function index($id)
{
$client = new Client(['base_uri' => 'http://qpeople.me/']);
$response=$client->post('profileinfo', [
'json'=>[
'tshirtID'=>$id
]
]);
$body=$response->getBody();
dd($body);
return view('profile');
}
好吧,我找到了解决方案,通过使用卷曲发送请求并得到回应,这里是代码:
$url = 'http://qpeople.me/profileinfo';
$data['tshirtID'] =$id;
$_data = json_encode($data);
$headers = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($_data),
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
尝试:
<?php
$body = (string) $response->getBody();
dd($body);