cURL登录ebay.co.uk [英] cURL login into ebay.co.uk
问题描述
我一直在尝试一段时间使用cURL登录eBay.co.uk。正在设置cookie,并且发送数据正确发送,但我不能确定cookie文件是否正在被重新读取,或者甚至设置正确,因为我得到一个页面从eBay说,我需要启用cookies我的浏览器。
这里是我使用的cURL类:
class Curl {
private $ ch;
private $ cookie_path;
private $ agent;
public function __construct($ userId){
$ this-> cookie_path = dirname(realpath(basename($ _ SERVER ['PHP_SELF']))'/ cookies /'。 $ userId。 '。文本';
$ this-> agent =Mozilla / 5.0(Windows; U; Windows NT 5.0; en-US; rv:1.4)Gecko / 20030624 Netscape / 7.1
}
私人函数init(){
$ this-> ch = curl_init();
}
私人函数close(){
curl_close($ this-> ch);
}
私人函数setOptions($ submit_url){
curl_setopt($ this-> ch,CURLOPT_URL,$ submit_url);
curl_setopt($ this-> ch,CURLOPT_USERAGENT,$ this-> agent);
curl_setopt($ this-> ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ this-> ch,CURLOPT_FOLLOWLOCATION,1);
// curl_setopt($ this-> ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ this-> ch,CURLOPT_COOKIEFILE,$ this-> cookie_path);
curl_setopt($ this-> ch,CURLOPT_COOKIEJAR,$ this-> cookie_path);
}
public function curl_cookie_set($ submit_url){
$ this-> init();
$ this-> setOptions($ submit_url);
$ result = curl_exec($ this-> ch);
$ this-> close();
return $ result;
}
public function curl_post_request($ referer,$ submit_url,$ data){
$ this-> init();
$ this-> setOptions($ submit_url);
$ post = http_build_query($ data);
curl_setopt($ this-> ch,CURLOPT_POST,1);
curl_setopt($ this-> ch,CURLOPT_POSTFIELDS,$ post);
curl_setopt($ this-> ch,CURLOPT_REFERER,$ referer);
$ result = curl_exec($ this-> ch);
$ this-> close();
return $ result;
}
public function curl_clean(){
//清除并关闭curl连接
if(file_exists($ this-> cookie_path)){
unlink($ this-> cookie_path);
}
if($ this-> ch!=''){
curl_close($ this-> ch);
}
}
}
这里是测试脚本,
$ curl = new Curl(md5(1) )); //(md5($ _ SESSION ['userId']));
$ referer ='http://ebay.co.uk';
$ submit_url =http://signin.ebay.co.uk/aw-cgi/eBayISAPI.dll;
$ data ['userid'] =VitoGambino-us;
$ data ['pass'] =P0wqw12vi;
$ data ['MfcISAPICommand'] ='SignInWelcome';
$ data ['siteid'] ='0';
$ data ['co_partnerId'] ='2';
$ data ['UsingSSL'] ='0';
$ data ['ru'] ='';
$ data ['pp'] ='';
$ data ['pa1'] ='';
$ data ['pa2'] ='';
$ data ['pa3'] ='';
$ data ['i1'] ='-1';
$ data ['pageType'] ='-1';
$ curl-> curl_cookie_set($ referer);
$ result = $ curl-> curl_post_request($ referer,$ submit_url,$ data);
echo $ result;
以下是cookie文件的内容:
#Netscape HTTP Cookie文件
#http://curl.haxx.se/rfc/cookie_spec.html
#此文件由libcurl生成!编辑自行承担风险。
www.ebay.co.uk FALSE / FALSE 0 JSESSIONID BDE9B23B829CA7DF2CC4D5880F5173A6
.ebay.co.uk TRUE / FALSE 0 ebay%5Esbf%3D%23%5Ecv%3D15555%5E
.ebay.co.uk TRUE / FALSE 1431871451 DP1 bu1p / QEBfX0BAX19AQA ** 53776c5b ^
#HttpOnly_.ebay.co.uk TRUE / FALSE 0秒CgAD4ACBRl4pbYjJjZDk1YTAxM2UwYTU2YjYzYzRhYmU0ZmY2ZjcyODYBSgAXUZeKWzUxOTYzOGI5LjMuMS43LjY2LjguMC4xuMWzLg **
.ebay.co.uk TRUE / FALSE 1400335451 nonsession CgADLAAFRlj / jMgDKACBa / DpbYjJjZDk1YTAxM2UwYTU2YjYzYzRhYmU0ZmY2ZjcyODcBTAAXU3dsWzUxOTYzOGI5LjMuMS42LjY1LjEuMC4xhVUTMQ **
.ebay.co.uk TRUE / FALSE 1526479451 lucky9 4551358
eBay使用一种非常棘手的方法登录。
这是我如何解决这个问题。
新修改的类:
class Curl {
private $ ch;
private $ cookie_path;
private $ agent;
//用户ID稍后将用于保持多个用户记录
//一次进入ebay站点。
public function __construct($ userId){
$ this-> cookie_path = dirname(realpath(basename($ _ SERVER ['PHP_SELF']))'/ cookies /'。 $ userId。 '。文本';
$ this-> agent =Mozilla / 5.0(Windows; U; Windows NT 5.0; en-US; rv:1.4)Gecko / 20030624 Netscape / 7.1
}
私人函数init(){
$ this-> ch = curl_init();
}
私人函数close(){
curl_close($ this-> ch);
}
//设置cURL选项
私有函数setOptions($ submit_url){
$ headers [] =Accept:* / *
$ headers [] =连接:Keep-Alive;
curl_setopt($ this-> ch,CURLOPT_URL,$ submit_url);
curl_setopt($ this-> ch,CURLOPT_USERAGENT,$ this-> agent);
curl_setopt($ this-> ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ this-> ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ this-> ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ this-> ch,CURLOPT_HTTPHEADER,$ headers);
curl_setopt($ this-> ch,CURLOPT_COOKIEFILE,$ this-> cookie_path);
curl_setopt($ this-> ch,CURLOPT_COOKIEJAR,$ this-> cookie_path);
}
//获取初始cookie数据
public function curl_cookie_set($ submit_url){
$ this-> init();
$ this-> setOptions($ submit_url);
curl_exec($ this-> ch);
echo curl_error($ this-> ch);
}
//隐藏字段
public function get_form_fields($ submit_url){
curl_setopt($ this-> ch,CURLOPT_URL,$ submit_url);
$ result = curl_exec($ this-> ch);
echo curl_error($ this-> ch);
return $ this-> getFormFields($ result);
}
//发送登录数据
public function curl_post_request($ referer,$ submit_url,$ data){
$ post = http_build_query($ data);
curl_setopt($ this-> ch,CURLOPT_URL,$ submit_url);
curl_setopt($ this-> ch,CURLOPT_POST,1);
curl_setopt($ this-> ch,CURLOPT_POSTFIELDS,$ post);
curl_setopt($ this-> ch,CURLOPT_REFERER,$ referer);
$ result = curl_exec($ this-> ch);
echo curl_error($ this-> ch);
$ this-> close();
return $ result;
}
//显示登录的My eBay或任何其他页面
public function show_page($ submit_url){
curl_setopt($ this-> ch,CURLOPT_URL,$ submit_url);
$ result = curl_exec($ this-> ch);
echo curl_error($ this-> ch);
return $ result;
}
//用于解析形式
私有函数getFormFields($ data){
if(preg_match('/(< form name =SignInForm *。< \ / form>)/ is',$ data,$ matches)){
$ inputs = $ this-> getInputs($ matches [1]);
return $ inputs;
} else {
die('Form not found。');
}
}
//用于解析隐藏的字段名和值
私有函数getInputs($ form){
$ inputs = array );
$ elements = preg_match_all('/(< input [^>] +>)/ is',$ form,$ matches);
if($ elements> 0){
for($ i = 0; $ i< $ elements; $ i ++){
$ el = preg_replace \s {2,} /','',$ matches [1] [$ i]);
if(preg_match('/ name =(?:[\'])?([^\'\s] *)/ i',$ el,$ name) ){
$ name = $ name [1];
$ value ='';
if(preg_match('/ value =(?:[\'])?([^\'\s] *)/ i',$ el,$ value) ){
$ value = $ value [1];
}
$ inputs [$ name] = $ value;
}
}
}
return $ inputs;
}
//销毁cookie并关闭curl。
public function curl_clean(){
//清除并关闭curl连接
if(file_exists($ this-> cookie_path)){
unlink($ this-> cookie_path);
}
if($ this-> ch!=''){
curl_close($ this-> ch);
}
}
}
$ curl = new Curl(md5(1)); //(md5($ _ SESSION ['userId']));
$ referer ='http://ebay.com';
$ formPage ='http://signin.ebay.com/aw-cgi/eBayISAPI.dll?SignIn';
//从主页面获取Cookie,ebay.com
$ curl-> curl_cookie_set($ referer);
//获取隐藏的表单字段,然后设置UsingSSL = 0
//使用凭据和隐藏字段登录
$ data = $ curl-> get_form_fields($ formPage) ;
$ data ['userid'] =;
$ data ['pass'] =;
$ data ['UsingSSL'] ='0';
//将数据写入登录页面。不要回显这个结果,有一个
// javascript重定向。只需执行此操作即可保存登录Cookie
$ formLogin =https://signin.ebay.com/ws/eBayISAPI.dll?co_partnerId=2&siteid=3&UsingSSL=0;
$ curl-> curl_post_request($ referer,$ formLogin,$ data);
//使用登录cookies加载我的eBay页面,中提琴,您已登录。
$ result = $ curl-> show_page('http:// my .ebay.com / ws / eBayISAPI.dll?MyeBay');
//取出Javascript,所以它不会重定向到actualy ebay网站
echo str_replace('< script','<',$ result);
I've been trying for some time now to use cURL to login to eBay.co.uk. The cookies are being set and the post data is being sent correctly however I am not sure that the cookie file is being read again or even set correctly for that matter since I'm getting a page from eBay that says I need to enable cookies in my browser.
Here is the cURL class I'm using:
class Curl {
private $ch;
private $cookie_path;
private $agent;
public function __construct($userId) {
$this->cookie_path = dirname(realpath(basename($_SERVER['PHP_SELF']))).'/cookies/' . $userId . '.txt';
$this->agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
}
private function init() {
$this->ch = curl_init();
}
private function close() {
curl_close ($this->ch);
}
private function setOptions($submit_url) {
curl_setopt($this->ch, CURLOPT_URL, $submit_url);
curl_setopt($this->ch, CURLOPT_USERAGENT, $this->agent);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookie_path);
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookie_path);
}
public function curl_cookie_set($submit_url) {
$this->init();
$this->setOptions($submit_url);
$result = curl_exec ($this->ch);
$this->close();
return $result;
}
public function curl_post_request($referer, $submit_url, $data) {
$this->init();
$this->setOptions($submit_url);
$post = http_build_query($data);
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($this->ch, CURLOPT_REFERER, $referer);
$result = curl_exec ($this->ch);
$this->close();
return $result;
}
public function curl_clean() {
// cleans and closes the curl connection
if (file_exists($this->cookie_path)) {
unlink($this->cookie_path);
}
if ($this->ch != '') {
curl_close ($this->ch);
}
}
}
Here is the test script, the login details are for a throwaway account, so feel free to test with them:
$curl = new Curl(md5(1)); //(md5($_SESSION['userId']));
$referer = 'http://ebay.co.uk';
$submit_url = "http://signin.ebay.co.uk/aw-cgi/eBayISAPI.dll";
$data['userid'] = "VitoGambino-us";
$data['pass'] = "P0wqw12vi";
$data['MfcISAPICommand'] = 'SignInWelcome';
$data['siteid'] = '0';
$data['co_partnerId'] = '2';
$data['UsingSSL'] = '0';
$data['ru'] = '';
$data['pp'] = '';
$data['pa1'] = '';
$data['pa2'] = '';
$data['pa3'] = '';
$data['i1'] = '-1';
$data['pageType'] = '-1';
$curl->curl_cookie_set($referer);
$result = $curl->curl_post_request($referer, $submit_url, $data);
echo $result;
Here is what the cookie files contents are:
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
www.ebay.co.uk FALSE / FALSE 0 JSESSIONID BDE9B23B829CA7DF2CC4D5880F5173A6
.ebay.co.uk TRUE / FALSE 0 ebay %5Esbf%3D%23%5Ecv%3D15555%5E
.ebay.co.uk TRUE / FALSE 1431871451 dp1 bu1p/QEBfX0BAX19AQA**53776c5b^
#HttpOnly_.ebay.co.uk TRUE / FALSE 0 s CgAD4ACBRl4pbYjJjZDk1YTAxM2UwYTU2YjYzYzRhYmU0ZmY2ZjcyODYBSgAXUZeKWzUxOTYzOGI5LjMuMS43LjY2LjguMC4xuMWzLg**
.ebay.co.uk TRUE / FALSE 1400335451 nonsession CgADLAAFRlj/jMgDKACBa/DpbYjJjZDk1YTAxM2UwYTU2YjYzYzRhYmU0ZmY2ZjcyODcBTAAXU3dsWzUxOTYzOGI5LjMuMS42LjY1LjEuMC4xhVUTMQ**
.ebay.co.uk TRUE / FALSE 1526479451 lucky9 4551358
I was able to figure it out.
eBay uses a pretty tricky method for logging in. It's a combination of cookies, hidden fields and a javascript redirect after successful login.
Here's how I solved it.
Newly modified class:
class Curl {
private $ch;
private $cookie_path;
private $agent;
// userId will be used later to keep multiple users logged
// into ebay site at one time.
public function __construct($userId) {
$this->cookie_path = dirname(realpath(basename($_SERVER['PHP_SELF']))).'/cookies/' . $userId . '.txt';
$this->agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
}
private function init() {
$this->ch = curl_init();
}
private function close() {
curl_close ($this->ch);
}
// Set cURL options
private function setOptions($submit_url) {
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
curl_setopt($this->ch, CURLOPT_URL, $submit_url);
curl_setopt($this->ch, CURLOPT_USERAGENT, $this->agent);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookie_path);
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookie_path);
}
// Grab initial cookie data
public function curl_cookie_set($submit_url) {
$this->init();
$this->setOptions($submit_url);
curl_exec ($this->ch);
echo curl_error($this->ch);
}
// Grab hidden fields
public function get_form_fields($submit_url) {
curl_setopt($this->ch, CURLOPT_URL, $submit_url);
$result = curl_exec ($this->ch);
echo curl_error($this->ch);
return $this->getFormFields($result);
}
// Send login data
public function curl_post_request($referer, $submit_url, $data) {
$post = http_build_query($data);
curl_setopt($this->ch, CURLOPT_URL, $submit_url);
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($this->ch, CURLOPT_REFERER, $referer);
$result = curl_exec ($this->ch);
echo curl_error($this->ch);
$this->close();
return $result;
}
// Show the logged in "My eBay" or any other page
public function show_page( $submit_url) {
curl_setopt($this->ch, CURLOPT_URL, $submit_url);
$result = curl_exec ($this->ch);
echo curl_error($this->ch);
return $result;
}
// Used to parse out form
private function getFormFields($data) {
if (preg_match('/(<form name="SignInForm".*?<\/form>)/is', $data, $matches)) {
$inputs = $this->getInputs($matches[1]);
return $inputs;
} else {
die('Form not found.');
}
}
// Used to parse out hidden field names and values
private function getInputs($form) {
$inputs = array();
$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
if ($elements > 0) {
for($i = 0; $i < $elements; $i++) {
$el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
$name = $name[1];
$value = '';
if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
$value = $value[1];
}
$inputs[$name] = $value;
}
}
}
return $inputs;
}
// Destroy cookie and close curl.
public function curl_clean() {
// cleans and closes the curl connection
if (file_exists($this->cookie_path)) {
unlink($this->cookie_path);
}
if ($this->ch != '') {
curl_close ($this->ch);
}
}
}
The actual code in use:
$curl = new Curl(md5(1)); //(md5($_SESSION['userId']));
$referer = 'http://ebay.com';
$formPage = 'http://signin.ebay.com/aw-cgi/eBayISAPI.dll?SignIn';
// Grab cookies from main page, ebay.com
$curl->curl_cookie_set($referer);
// Grab the hidden form fields and then set UsingSSL = 0
// Login with credentials and hidden fields
$data = $curl->get_form_fields($formPage);
$data['userid'] = "";
$data['pass'] = "";
$data['UsingSSL'] = '0';
// Post data to login page. Don't echo this result, there's a
// javascript redirect. Just do this to save login cookies
$formLogin = "https://signin.ebay.com/ws/eBayISAPI.dll?co_partnerId=2&siteid=3&UsingSSL=0";
$curl->curl_post_request($referer, $formLogin, $data);
// Use login cookies to load the "My eBay" page, viola, you're logged in.
$result = $curl->show_page('http://my.ebay.com/ws/eBayISAPI.dll?MyeBay');
// take out Javascript so it won't redirect to actualy ebay site
echo str_replace('<script', '<', $result);
I used some of the code posted here, thanks to drew010!
这篇关于cURL登录ebay.co.uk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!