douzen1880 2017-04-23 07:44
浏览 90
已采纳

guzzle使用登录表单进行身份验证

Is it possible to authenticate a user with a login form for example login into GitHub via https://github.com/login and then go to a page that needs login for example https://github.com/settings/profile? I know packages like "FriendsOfPHP/Goutte" do this job but they use "symfony/browser-kit" package with guzzle. Is it possible to do this with guzzle only? I tried this but the login does not authenticate:

<?php

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Cookie\FileCookieJar;

class guzzleRequest {

    private $client;
    private $jar;
    private $body;
    private $baseUrl;
    private $host;
    private $path;

    public function __construct($base_url, $host) {
        $this->path = __DIR__ . '/tmp/cookie.txt';
        $this->jar = new FileCookieJar($this->path);
        $this->client = new Client(['cookies' => $this->jar, 'allow_redirects' => TRUE]);
        $this->baseUrl = $base_url;
        $this->host = $host;
    }

    protected function makeRequest($page, $query = array(), $debug = false) {
        try {
            $options = [
                'headers' => [
                    'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
                    'Host' => $this->host,
                    'Referer' => $this->baseUrl . $page
                ]
            ];

            if (!empty($query)) {
                $options['query'] = $query;
            }

            $res = $this->client->request('GET', $this->baseUrl . $page, $options);
            $this->body = $res->getBody();

            return true;
        } catch (RequestException $e) {
            $this->body = $e->getResponse()->getBody();
            return false;
        }
    }

    public function getPage($page) {

        $this->makeRequest($page);
        return $this->body;
    }

    public function submitForm($page, $query = array(), $form_params = array(), $debug = false) {
        try {
            $options = [
                'headers' => [
                    'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
                    'Host' => $this->host,
                    'Referer' => $this->baseUrl . $page
                ]
            ];

            if (!empty($query)) {
                $options['query'] = $query;
            }

            if (!empty($form_params)) {
                $options['form_params'] = $form_params;
            }

            $res = $this->client->request('POST', $this->baseUrl . $page, $options);
            $this->body = $res->getBody();
            $this->jar->save($this->path);
            return true; //everything is fine
        } catch (RequestException $e) {
            $this->body = $e->getResponse()->getBody();
            return false;
        }
    }

    public function saveBody($mod = '') {
        file_put_contents('tmp/output' . $mod . '.html', $this->body);
    }

    public function loadBody() {
        $this->body = file_get_contents('test/output.html');
        return $this->body;
    }

}

and then call it:

require_once 'vendor/autoload.php';
require_once 'guzzleRequest.php';

$guzzleRequest = new guzzleRequest("https://github.com", "github.com");
$form_params = ["login" => "user", "password" => "pass"];
$guzzleRequest->submitForm('/session', array(), $form_params, true);
$guzzleRequest->getPage('/settings/profile');
$guzzleRequest->saveBody();

but in the output.html, the save page is "github.com/login" and the user not authenticated

  • 写回答

1条回答 默认 最新

  • douzhan3900 2017-06-13 21:10
    关注

    i opened an issue in github, and got answer that it is not possible to do this with guzzle only "https://github.com/guzzle/guzzle/issues/1817"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条