Translate cURL request to Guzzle

I am trying to use Guzzle instead of using cURL directly to achieve and HTTP request. How to make the same type of request, but using Guzzle? Or should I just stick with cURL?

$ch = curl_init(); // Set the URL curl_setopt($ch, CURLOPT_URL, $url); // don't verify SSL certificate curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // Return the contents of the response as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Follow redirects curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // Set up authentication curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, "$token:X"); 

This is how I got it. I continue to work with error 401 Unauthorized. I know that I have the correct credentials. What makes me think that I'm not on the right track is the Guzzle: auth docs currently only supported using the cURL handler, but a replacement is planned that can be used with any HTTP handler. But from my understanding Guzzle uses cURL by default.

 $guzzleData = [ 'auth' => [$token, 'X'], 'allow_redirects' => true, 'verify' => false, ]; $client = new \Guzzle\Http\Client(); $request = $client->get($url, $guzzleData); $response = $request->send(); 
+5
source share
2 answers

Here is the solution:

 $client = new \Guzzle\Http\Client(); $request = $client->get($url); $request->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false); $request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false); $request->getCurlOptions()->set(CURLOPT_RETURNTRANSFER, true); $request->getCurlOptions()->set(CURLOPT_FOLLOWLOCATION, true); $request->getCurlOptions()->set(CURLOPT_HTTPAUTH, CURLAUTH_BASIC); $request->getCurlOptions()->set(CURLOPT_USERPWD, "$token:X"); $response = $request->send(); 
+9
source

The solution I was able to get for Guzzle6 is:

 $headers = array(); $headers['grant_type'] = 'client_credentials'; $headers['client_id'] = $clientid; $headers['client_secret'] = $clientSecret; $response = $this->client->post($urlAuth, ['form_params' => $headers]); $output = $response->getBody()->getContents(); 

those. array of headers should be wrapped in 'form_params'

0
source

All Articles