将此cURL转换为Guzzle Post

时间:2017-04-12 14:15:00

标签: php curl

如何将此跟随cURL转换为Guzzle?

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8"); // new
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$return = curl_exec($ch);
curl_close($ch);
return $return;

我试试这种方式。但是不起作用:

$client = new GuzzleHttp\Client();
$response = $client->post($url, ['json' => $data]);

return $response;

1 个答案:

答案 0 :(得分:1)

如果您将数据作为json选项传递,Guzzle会将此数组编码为JSON并在请求正文中发送JSON。使用form_params代替json