【问题标题】:guzzle client throws exception in laravelguzzle 客户端在 laravel 中抛出异常
【发布时间】:2016-06-24 10:22:16
【问题描述】:

我正在尝试在 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',
            ]
        ]);

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

curl_setopt_array():不能将 Output 类型的流表示为 STDIO FILE*

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

知道怎么解决吗?

【问题讨论】:

  • 您是否正在向您有权访问的服务发出 POST 请求?您可以调试那里收到的内容以及它是否返回任何数据?
  • 是的,当我执行 $res->getBody() 时,它是一个发布请求,它返回一些未显示任何预期输出的数据。我正在努力。
  • 如果不查看完整的错误输出,您似乎正在目睹与此 Guzzle 错误帖子github.com/guzzle/guzzle/issues/1413中提到的相同问题@

标签: laravel guzzle


【解决方案1】:

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

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

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

More information on using Guzzle Reponse object here

【讨论】:

    【解决方案2】:

    我必须这样做

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

    【讨论】:

      【解决方案3】:

      这是我为我的 SMS 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'));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-21
        • 2020-11-20
        • 2011-11-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多