【问题标题】:laravel5 and GuzzleHttplaravel5 和 GuzzleHttp
【发布时间】:2016-02-10 11:37:49
【问题描述】:

我正在使用 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');
}

这是回复

【问题讨论】:

  • 我不知道 laravel 的辅助函数,但是你执行 dd($response->getBody()->__toString()) 会得到什么?

标签: php laravel laravel-5 guzzle guzzle6


【解决方案1】:

尝试:

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

【讨论】:

  • 不鼓励使用纯代码的答案。请单击edit 并添加一些词来总结您的代码如何解决问题,或者解释您的答案与之前的答案/答案有何不同。 From Review
【解决方案2】:

好的,我通过使用 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    相关资源
    最近更新 更多