【问题标题】:How can I get response from guzzle 6 in Laravel 5.3?如何从 Laravel 5.3 中的 guzzle 6 获得响应?
【发布时间】:2017-10-24 08:52:41
【问题描述】:

我从这里读到:http://www.phplab.info/categories/laravel/consume-external-api-from-laravel-5-using-guzzle-http-client

我尝试这样:

...
use GuzzleHttp\Client as GuzzleHttpClient;
use GuzzleHttp\Exception\RequestException;
...
public function testApi()
{
    try {
        $client = new GuzzleHttpClient();
        $apiRequest = $client->request('POST', 'https://myshop/api/auth/login', [
            // 'query' => ['plain' => 'Ab1L853Z24N'],
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
            'auth' => ['test@gmail.com', '1234'],       //If authentication required
            // 'debug' => true                                  //If needed to debug   
        ]);
        $content = json_decode($apiRequest->getBody()->getContents());
        dd($content);
    } catch (RequestException $re) {
          //For handling exception
    }
}

执行时,结果为空

我怎样才能得到响应?

我在邮递员中尝试,它成功得到响应

但我尝试使用 guzzle,它失败了

更新:

我检查了邮递员,结果有效

我尝试点击邮递员上的按钮代码

然后我选择php curl并复制它,结果如下:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://myshop/api/auth/login",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"email\"\r\n\r\ntest@gmail.com\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\n1234\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    "postman-token: 1122334455-abcd-edde-aabe-adaddddddddd"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

如果它使用curl php,那么代码是这样的

如果使用 guzzle,我如何获得响应?

【问题讨论】:

  • 为什么我的问题被否决了?有错吗?

标签: php laravel laravel-5.3 guzzle guzzle6


【解决方案1】:

我看到至少一个语法错误。 request() 方法的第三个参数应该是这样的:

$requestContent = [
    'headers' = [],
    'json' = []
];

你的情况可能是:

public function testApi()
{
    $requestContent = [
        'headers' => [
            'Accept' => 'application/json',
            'Content-Type' => 'application/json'
        ],
        'json' => [
            'email' => 'test@gmail.com',
            'password' => '1234',
            // 'debug' => true
        ]
    ];

    try {
        $client = new GuzzleHttpClient();

        $apiRequest = $client->request('POST', 'https://myshop/api/auth/login', $requestContent);

        $response = json_decode($apiRequest->getBody());

        dd($response);
    } catch (RequestException $re) {
          // For handling exception.
    }
}

您的数据还有其他参数而不是json,例如form_params。我建议你看看the Guzzle documentation

【讨论】:

  • 应该将 $httpContent 改为 $requestContent
  • 结果为空
  • 是的,是一样的
  • 这很奇怪。我使用邮递员和phpcurl,它可以得到响应。好像有代码需要添加
  • 使用 Postman 的时候,用什么样的 body? form-datax-www-form-urlencoded?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 2018-05-29
  • 2015-08-13
  • 2016-05-12
  • 1970-01-01
  • 2023-03-10
相关资源
最近更新 更多