如何解决无法登录到远程站点cookie的问题?

时间:2019-01-12 18:55:08

标签: php cookies

我正在使用php file_get_contentsdom documment登录到第三方网站(gmail)。但是,当我运行代码时,它将显示cookie问题文本页面:Turn cookies on or off,这意味着我未启用cookie。但是我已经使用此代码$http_response_header启用了它,请查看下面的代码,您将了解。

我不想用卷发。卷曲支持cookie,但file_get_contents不支持cookie。我只想使用php进行远程登录,所以我不使用curl。我不确切地知道我的错误在哪里以及为什么在代码上添加cookie函数后为什么显示cookie问题。失败之后,我在这里提交了这篇文章,以寻求天才的解决方案。

这是我的gmail.php代码:

<?php

class LoginGmail
{
  public $request_cookies = '';
  public $response_cookies = '';
  public $content = '';

  public function set_cookies_json($cookies)
  {
    $cookies_json = json_decode($cookies, true);
    $cookies_array = array();
    foreach ($cookies_json as $key => $value)
    {
      $cookies_array[] = $key .'='.$value;
    }
    $this->request_cookies = 'Cookie: ' . join('; ', $cookies_array) . "\r\n";
  }

  public function set_cookies_string($cookies)
  {
    $this->request_cookies = 'Cookie: ' . $cookies . "\r\n";
  }

  private function get_cookies($http_response_header)
  {
    $cookies_array = array();
    foreach($http_response_header as $s)
    {
      if (preg_match('|^Set-Cookie:\s*([^=]+)=([^;]+);(.+)$|', $s, $parts))
      {
        $cookies_array[] = $parts[1] . '=' . $parts[2];
      }
    }

    $this->response_cookies = 'Cookie: ' . join('; ', $cookies_array) . "\r\n";
  }


  public function get($url)
  {
    $opts = array(
      'http' => array(
        'method' => 'GET',
        'header' => "Accept-language: en\r\n" .
                    "User-Agent: com.google.android.apps.maps/984200142 (Linux; U; Android 5.0; en_US; GT-I9500; Build/LRX21M; Cronet/66.0.3359.100)\r\n" .
                    $this->request_cookies
      )
    );
    $context = stream_context_create($opts);
    $this->content = file_get_contents($url, false, $context);
    $this->get_cookies($http_response_header);
    return $this->content;
  }

  public function post($url, $inputs)
  {
    $post_content = array();
    foreach ($inputs as $key => $value)
    {
      $post_content[] = $key .'='.$value;
    }

    $opts = array(
      'http' => array(
        'method' => 'POST',
        'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
                    "User-Agent: com.google.android.apps.maps/984200142 (Linux; U; Android 5.0; en_US; GT-I9500; Build/LRX21M; Cronet/66.0.3359.100)\r\n" .
                    $this->request_cookies,
        'content' => join('&', $post_content),
      )
    );
    $context = stream_context_create($opts);
    $this->content = file_get_contents($url, false, $context);
    $this->get_cookies($http_response_header);
    return $this->content;
  }


  public function postPass($url, $inputs)
  {
    $post_content = array();
    foreach ($inputs as $key => $value)
    {
      $post_content[] = $key .'='.$value;
    }

    $opts = array(
      'http' => array(
        'method' => 'POST',
        'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
                    "User-Agent: com.google.android.apps.maps/984200142 (Linux; U; Android 5.0; en_US; GT-I9500; Build/LRX21M; Cronet/66.0.3359.100)\r\n" .
                    $this->request_cookies,
        'content' => join('&', $post_content),
      )
    );
    $context = stream_context_create($opts);
    $this->content = file_get_contents($url, false, $context);
    $this->get_cookies($http_response_header);
    return $this->content;
  }
}
?>

这是我的login.php代码:

<?php

require_once('gmail.php');
$connect = new LoginGmail();

$url = 'https://store.google.com/account';
$first = $connect->get($url);

$domd = @DOMDocument::loadHTML($first);
$xp = new DOMXPath($domd);

foreach($domd->getElementsByTagName("form")->item(0)->getElementsByTagName("input") as $get)
{
    $inputs[$get->getAttribute("name")]=$get->getAttribute("value");
}

$inputs["Email"]="HERE_IS_GMAIL_ID";
$url = $xp->query("//form")->item(0)->getAttribute("action");
$second = $connect->post($url, $inputs);


$domd = @DOMDocument::loadHTML($second);
$xp = new DOMXPath($domd);

foreach($domd->getElementsByTagName("form")->item(0)->getElementsByTagName("input") as $get)
{
    $inputs[$get->getAttribute("name")]=$get->getAttribute("value");
}

$inputs["Passwd"]="HERE_IS_GMAIL_PASSWORD";
$url = $xp->query("//form")->item(0)->getAttribute("action");
echo $connect->postPass($url, $inputs);
?>

注意:我在代码中隐藏了我的个人电子邮件和密码。对不起,英语不好。谢谢

1 个答案:

答案 0 :(得分:0)

我认为您在Google上执行请求的方式不正确。您的代码没有错,这是您尝试进行身份验证的方式。查看this文档,也许会有所帮助。

相关问题