guzzle客户端在laravel抛出异常

时间:2016-03-09 20:23:29

标签: laravel guzzle

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

$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',
            ]
        ]);

它返回空响应。在调试时,发生以下异常

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

我正在使用最新版本的guzzle。

知道怎么解决吗?。

3 个答案:

答案 0 :(得分:2)

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

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

现在检查$ data中的内容以及它是否与预期输出相对应。

More information on using Guzzle Reponse object here

答案 1 :(得分:0)

我必须这样做

$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]);

答案 2 :(得分:0)

这是我为我的短信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'));
相关问题