guzzle 客户端在 laravel 中抛出异常

guzzle client throws exception in laravel

我正尝试在 laravel 中发出 http post 请求,如下所示

$client = new Client(['debug'=>true,'exceptions'=>false]);
  $res = $client->request('POST', 'http://www.myservice.com/find_provider.php',  [
            'form_params' => [
                'street'=> 'test',
                'apt'=> '',
                'zip'=> 'test',
                'phone'=> 'test',
            ]
        ]);

它return 空响应。调试时,出现以下异常

curl_setopt_array(): 无法将输出类型的流表示为 STDIO 文件*

我使用的是最新版本的 guzzle。

知道如何解决吗?

request() 方法返回一个 GuzzleHttp\Psr7\Response 对象。 要获取服务返回的实际数据,您应该使用:

$data = $res->getBody()->getContents();

现在检查 $data 中的内容以及它是否符合预期输出。

More information on using Guzzle Reponse object here

我不得不这样做

$data = $res->getBody()->getContents();<br>
but also change<br>
$client = new \GuzzleHttp\Client(['verify' => false, 'debug' => true]);<br>
to<br> 
$client = new \GuzzleHttp\Client(['verify' => false]);

这是我为短信所做的 api

use Illuminate\Support\Facades\Http; // Use this on top

// Sample Code
$response = Http::asForm()
           ->withToken(env('SMS_AUTH_TOKEN'))
           ->withOptions([
                         'debug'  => fopen('php://stderr', 'w') // Update This Line
            ])
            ->withHeaders([
                         'Cache-Control' => 'no-cache',
                         'Content-Type'  => 'application/x-www-form-urlencoded',
             ])
             ->post($apiUrl,$request->except('_token'));