1

I'm using Guzzle for http post requests, but I'm having problems with it redirecting after the code runs

use GuzzleHttp\Client;

$billclient = new Client();
$request = $billclient->post('http://posturl.com', [
    'body' => 'lorem ipsum'
]);

$redirect = new Client();
$redirect->get('http://google.com');

header("Location: http://google.com");

If I am running multiple Guzzle requests it doesn't seem to want to load, and instead the script ends. I really need to redirect to another page because it passes a query string that fills in the forms on the next page (second call to action)

EDIT: It seems like Guzzle closes off all handles as soon as a new Client is initiated. The code works fine if I remove the New Client() portion of the code. I've gone back to using CURL requests for now, but this isn't a very good long term solution since it makes the code very fragile.

Any ideas?

Andrew Seipp
  • 21
  • 3
  • 4
  • What errors are you getting, if any? See http://stackoverflow.com/q/845021/3794472 – Jeremiah Winsley Jan 11 '15 at 23:25
  • In the interpretor: Process finished with exit code 0, in the browswer it is Error code: ERR_EMPTY_RESPONSE – Andrew Seipp Jan 11 '15 at 23:53
  • 1
    Do you think you are being redirected from http to https? – botero Jan 12 '15 at 03:39
  • so I added a $header = header("Location: http://google.com"); and then did a VAR_DUMP($header) and it is comming back as NULL. It's almost as if the guzzle is stopping any additional paramaters from being created. Case in point, I have to put all other commands above Guzzle for them to work properly. – Andrew Seipp Jan 12 '15 at 03:50
  • Also, if I hit the refresh button on the white screen it redirects properly but then the post variables don't work – Andrew Seipp Jan 12 '15 at 17:51
  • @AndrewSeipp, `header()` does not return anything. Hence `$header` is NULL. – ragol Jan 12 '15 at 21:47
  • What version of Guzzle do you use? – ragol Jan 12 '15 at 21:50
  • @ragol version is 5.1.0 – Andrew Seipp Jan 13 '15 at 00:09
  • Do you have to run the requests synchronously? If not you could use asynchronous requests with promises. Check out http://docs.guzzlephp.org/en/latest/clients.html – ragol Jan 13 '15 at 08:52
  • Guzzle may throw exceptions which are prematurely ending the execution of your script; you should handle them. Also, there is no need to use multiple clients if you want to make single, unrelated requests. Just use the static methods `GuzzleHttp\get(), GuzzleHttp\post()`, etc. –  Jan 16 '15 at 21:17

0 Answers0