Reputation: 2051
I'm trying to use Guzzle in my project to read a value from a URL. The requested url only returns a number, no html header, body or anything. AT first I just used curl to read it, and already figured out I needed to set 2 extra cUrl options to do a succesfull read. My code looked like this, and worked like a charm:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://192.168.2.5/temp');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $value = curl_exec($ch);
Now that I am moving towards using guzzle, I thought I would be ok if I would just use the same cUrl options, so I created this code:
$client = new Client( );
$res = $client->request('GET','http://192.168.2.5/temp',['connect_timeout' => 10,'curl' => [CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => true]]);
echo $Value = $res->getBody()->read(1024);
However, this code is giving me this error:
RequestException in CurlFactory.php line 187: cURL error 0: The cURL request was retried 3 times and did not succeed. The most likely reason for the failure is that cURL was unable to rewind the body of the request and subsequent retries resulted in the same error. Turn on the debug option to see what went wrong. See https://bugs.php.net/bug.php?id=47204 for more information. (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
Any idea why this is not working?
Upvotes: 2
Views: 5142
Reputation: 143
My guess would be the CURLOPT_RETURNTRANSFER
because I found this question looking for answers to the same problem.
Eventually got my answer at the bottom of this Github issue: https://github.com/guzzle/guzzle/issues/2048
TL;DR – Guzzle is clobbering CURLOPT_FILE
and CURLOPT_WRITEFUNCTION
when CURLOPT_RETURNTRANSFER
is set; more details in Reading curl request progress headers with Guzzle
Upvotes: 0
Reputation: 126
This works fine for me (using guzzle 6.2.0):
$client = new \GuzzleHttp\Client();
$response = $client->request('GET','http://google.com');
echo $response->getBody()->getContents();
Alternatively, take a look at this post, and check if your server returns somthing similar, that may cause the problem.
Upvotes: 1