Php 使用cURL自动登录网站
大家好。我一直在寻找能帮我解决这个问题的方法 我正在编写一个脚本,将登录Gamefly.com,并向我的队列中添加50个游戏。队列部分工作得很好,但登录根本不起作用。我以前读过关于使用cURL的文章,我注意到PHP在使用它方面有足够的时间。下面是我的想法:Php 使用cURL自动登录网站,php,curl,Php,Curl,大家好。我一直在寻找能帮我解决这个问题的方法 我正在编写一个脚本,将登录Gamefly.com,并向我的队列中添加50个游戏。队列部分工作得很好,但登录根本不起作用。我以前读过关于使用cURL的文章,我注意到PHP在使用它方面有足够的时间。下面是我的想法: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://www.gamefly.com/member/logon/?ReturnUrl=http%3a%2f%2fwww.gamefl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.gamefly.com/member/logon/?ReturnUrl=http%3a%2f%2fwww.gamefly.com%2f');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'ctl00$ctl00$ctl00$MainContent$MainContent$MainContent$LoginForm$emailAddress=xxxx@xxxx.com&ctl00$ctl00$ctl00$MainContent$MainContent$MainContent$LoginForm$password=xxxx');
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
$test = curl_exec($ch);
显然,我认为我这样做时得到的字段是错误的。有没有人对我如何让它登录有任何想法,或者我是骨瘦如柴?谢谢。试试这个:
//Curl.php
class Curl {
public $cookieJar = "";
// Make sure the cookies.txt file is read/write permissions
public function __construct($cookieJarFile = '/var/www/html/cookies.txt') {
$this->cookieJar = $cookieJarFile;
}
function setup() {
$header = array();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // browsers keep this blank.
curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7');
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookieJar);
curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookieJar);
curl_setopt($this->curl, CURLOPT_AUTOREFERER, true);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
}
function get($url) {
$this->curl = curl_init($url);
$this->setup();
return $this->request();
}
function getAll($reg, $str) {
preg_match_all($reg, $str, $matches);
return $matches[1];
}
function postForm($url, $fields, $referer = '') {
$this->curl = curl_init($url);
$this->setup();
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_REFERER, $referer);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields);
return $this->request();
}
function getInfo($info) {
$info = ($info == 'lasturl') ? curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL) : curl_getinfo($this->curl, $info);
return $info;
}
function request() {
return curl_exec($this->curl);
}
}
如何致电:
//Login.php
include('/var/www/html/curl.php'); // This path would change to where you store the file
$curl = new Curl();
$url = "https://www.gamefly.com/member/logon/?ReturnUrl=http%3a%2f%2fwww.gamefly.com%2f";
$fields = "ctl00$ctl00$ctl00$MainContent$MainContent$MainContent$LoginForm$emailAddress=xxxx@xxxx.com&ctl00$ctl00$ctl00$MainContent$MainContent$MainContent$LoginForm$password=xxxx";
// Calling URL
$referer = "http://www.gamefly.com";
$html = $curl->postForm($url, $fields, $referer);
echo $html; // This will show you the HTML of the current page you and logged into
试试这个:
//Curl.php
class Curl {
public $cookieJar = "";
// Make sure the cookies.txt file is read/write permissions
public function __construct($cookieJarFile = '/var/www/html/cookies.txt') {
$this->cookieJar = $cookieJarFile;
}
function setup() {
$header = array();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // browsers keep this blank.
curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7');
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookieJar);
curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookieJar);
curl_setopt($this->curl, CURLOPT_AUTOREFERER, true);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
}
function get($url) {
$this->curl = curl_init($url);
$this->setup();
return $this->request();
}
function getAll($reg, $str) {
preg_match_all($reg, $str, $matches);
return $matches[1];
}
function postForm($url, $fields, $referer = '') {
$this->curl = curl_init($url);
$this->setup();
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_REFERER, $referer);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields);
return $this->request();
}
function getInfo($info) {
$info = ($info == 'lasturl') ? curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL) : curl_getinfo($this->curl, $info);
return $info;
}
function request() {
return curl_exec($this->curl);
}
}
如何致电:
//Login.php
include('/var/www/html/curl.php'); // This path would change to where you store the file
$curl = new Curl();
$url = "https://www.gamefly.com/member/logon/?ReturnUrl=http%3a%2f%2fwww.gamefly.com%2f";
$fields = "ctl00$ctl00$ctl00$MainContent$MainContent$MainContent$LoginForm$emailAddress=xxxx@xxxx.com&ctl00$ctl00$ctl00$MainContent$MainContent$MainContent$LoginForm$password=xxxx";
// Calling URL
$referer = "http://www.gamefly.com";
$html = $curl->postForm($url, $fields, $referer);
echo $html; // This will show you the HTML of the current page you and logged into
这真的取决于网站,为什么你需要这样做,如果这是一个值得(非垃圾邮件原因)与gamefly开发人员检查,我相信他们会给你一个API使用gamefly没有API。我过去曾请求过,也曾恳求过。这是一个个人应用程序,我使用它来根据标准确定我想要在队列中的游戏。这真的取决于网站,为什么你需要这样做,如果这是一个值得(非垃圾邮件原因)与gamefly开发者检查的话,我肯定他们会给你一个API来使用gamefly没有API。我过去曾请求过,也曾恳求过。这是一个个人应用程序,我使用它来根据标准确定我想要在队列中玩什么游戏。我修改了代码以使用您编写的内容,但它不起作用。在这一点上我很确定,当我提交表单或其他类似的东西时,我有不正确的字段。我会看看我是否能弄明白。我修改了我的代码以使用您编写的代码,但它不起作用。在这一点上我很确定,当我提交表单或其他类似的东西时,我有不正确的字段。我看看能不能弄明白。