Dostosuj odpowiednio.
use GuzzleHttp\Client;
use GuzzleHttp\TransferStats;
use Masterminds\HTML5;
function logIn($login, $password, Client $client, HTML5 $dom_parser)
{
$response = $client->request('GET', 'https://mojastrona.com/login');
$dom = $dom_parser->loadHTML($response->getBody());
$inputs = $dom->getElementsByTagName('input');
$csrf_token = '';
foreach ($inputs as $input) {
/** @var DOMElement $input */
if ($input->getAttribute('name') === '_csrf_token') {
$csrf_token = $input->getAttribute('value');
}
}
if (empty($csrf_token)) {
throw new Exception('Unable to authenticate');
}
// http://stackoverflow.com/questions/30682307/how-to-read-the-response-effective-url-in-guzzle-6-0
$current_url = null;
$client->request('POST', 'https://mojastrona.com/login', [
'form_params' => [
'_email' => $login,
'_password' => $password,
'_csrf_token' => $csrf_token
],
'on_stats' => function (TransferStats $stats) use (&$current_url) {
$current_url = $stats->getEffectiveUri();
}
]);
// after successful login we should be redirected to https://mojastrona.com/
if ((string)$current_url !== "https://mojastrona.com/") {
throw new Exception('Authorization failed.');
}
return true;
}
// Use a shared client cookie jar for all requests.
$client = new Client([
'cookies' => true
]);
$dom_parser = new HTML5();
logIn('john_doe', 'jane123', $client, $dom_parser);