laravel5 和 GuzzleHttp
laravel5 and GuzzleHttp
我正在使用 GuzzleHttp 向外部发送请求 api 并获得响应,但返回的响应数据为空。当我在 advanced rest client 中测试 uri 和参数时,我得到了一个数据,那么为什么 Guzzle 响应是空的?!
如果可以,请你帮助我。
这是我的代码:
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');
}
这是回复
好的,我找到了使用 cURL 发送请求并获得响应的解决方案,
这是代码:
$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);
我正在使用 GuzzleHttp 向外部发送请求 api 并获得响应,但返回的响应数据为空。当我在 advanced rest client 中测试 uri 和参数时,我得到了一个数据,那么为什么 Guzzle 响应是空的?! 如果可以,请你帮助我。
这是我的代码:
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');
}
这是回复
好的,我找到了使用 cURL 发送请求并获得响应的解决方案, 这是代码:
$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);