Replies: 2 comments
-
Hey you tried using phphttp with https://docs.php-http.org/en/latest/clients/react-adapter.html and https://docs.php-http.org/en/latest/plugins/cookie.html?highlight=cookies ? |
Beta Was this translation helpful? Give feedback.
-
ReactPHP is all about doing things asynchronously, so you've come to the right place :-) ReactPHP's HTTP client allows you to send HTTP requests and process HTTP responses, both which may include HTTP cookie headers, so this is supported just fine. Make sure to process the incoming HTTP $browser = new React\Http\Browser();
$browser = $browser->withHeader('Cookie', 'foo=bar');
$response = React\Async\await($browser->get('https://example.com/'));
assert($response instanceof Psr\Http\Message\ResponseInterface);
var_dump($response->getHeaderLine('Set-Cookie'));
No magic involved so far :-) But you can always manually persist the HTTP cookies like suggested above. The mean reason for this is that not everybody wishes to honor HTTP cookies and persistence means different things to different people. Should this be stored in volatile memory (RAM), should this persist after the program restarts (disk?), should this persist across a cluster of servers (database?), should this even be reused for features like "privacy mode"? As such, ReactPHP does not currently automatically set any cookie headers. We're definitely interested in providing better interfaces to do so in the future, you can join the discussion here: reactphp/http#445. We welcome contributions, reach out if you want to support this project 👍
Guzzle implements its own loop and as such does not integrate well with non-blocking applications. If you want to integrate this with an async application, Guzzle might not be the right tool for the job. As @WyriHaximus suggested, you may also utilize HTTPlug as an adapter that works very similar to Guzzle, but uses ReactPHP's HTTP client under the hood. Its CookiePlugin should be able to take care of HTTP cookie headers and should also be compatible with ReactPHP afaict. See answer above: https://github.com/orgs/reactphp/discussions/496#discussioncomment-4925511 |
Beta Was this translation helpful? Give feedback.
-
Some background:
I've been searching for almost two weeks for a way to do a page fetch and store persistent cookies. There have been many examples, most for Guzzle 6 and older.
I'm currently using code similar to a post at the tail of this discussion.
I asked on StackOverflow for a better way, without a good response:
(How do use Guzzle 7 fully asynchronously with ReactPHP event loop without blocking for I/O?).
But when I integrate it into a discordPHP bot, it doesn't seem to work.
My question:
How does one do an async GET with cookies magically persisted for me and allow other event loop items to run while I wait?
Alternatively, am I wrong and the actual answer as of today is still "You must do it synchronously and just wait for for the server to reply or timeout?"
Beta Was this translation helpful? Give feedback.
All reactions