How to convert this follow cURL to 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;
I try this way. But doesn't work:
$client = new GuzzleHttp\Client();
$response = $client->post($url, ['json' => $data]);
return $response;
If you pass your data as json
option, Guzzle will encode this array as JSON and send JSON in request body. Use form_params
instead of json
.