2017-04-23 4 views
1

ログインフォームを使用してユーザーを認証することはできますか?たとえば、https://github.com/loginでGitHubにログインし、ログインが必要なページ(例:https://github.com/settings/profile)に移動できますか?私は "FriendsOfPHP/Goutte"のようなパッケージはこの仕事をしていることを知っていますが、彼らは "symfony/browser-kit"パッケージをグッと使っています。それはグズルだけでこれを行うことは可能ですか? 私はこれを試してみましたが、ログインが認証されない:guzzがログインフォームで認証する

<?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; 
    } 

} 

をして、それを呼び出す:

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(); 

しかしoutput.htmlで、保存ページでは「github.com/login」とありますユーザー私はgithubのに問題を開いて、これを行うことは不可能であることを答えてしまった

+0

は目を参照してください。 http://stackoverflow.com/questions/25089960/how-to-get-past-login-screen-on-guzzle-call –

+0

私は前にそのページを確認しましたが、うまくいかなかったので、 API認証。あなたはそれをどうやって行うのですか? @BhupeshKushwaha –

答えて

関連する問題