[英]laravel5 and GuzzleHttp
我正在使用GuzzleHttp將請求發送到外部api並獲取響應,但是返回的響應中的數據為空。 當我在高級 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.