简体   繁体   English

laravel5和GuzzleHttp

[英]laravel5 and GuzzleHttp

I am using GuzzleHttp to send request to external api and get response, but the response returned is empty from data. 我正在使用GuzzleHttp将请求发送到外部api并获取响应,但是返回的响应中的数据为空。 and when i test a uri and parameters in advanced rest client i get a data,So why Guzzle response is empty?! 当我在高级 Rest Client中测试uri和参数时,我得到了数据,那为什么Guzzle响应为空? please help me if you can. 如果可以,请你帮助我。

here is my code: 这是我的代码:

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');
}

this is the response 这是回应 在此处输入图片说明

Ok,I found the solution by using cURL to send the request and get a response, here is the code: 好的,我通过使用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);

try: 尝试:

<?php
$body = (string) $response->getBody();
dd($body);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM