2013-05-17 3 views
0

Я уже некоторое время пытаюсь использовать cURL для входа на eBay.co.uk. Печеньки устанавливаются, и почтовые данные отправляются правильно, но я не уверен, что файл cookie снова читается или даже правильно настроен, поскольку я получаю страницу с eBay, в которой говорится, что мне нужно включить файлы cookie в мой браузер.cURL Войти на ebay.co.uk

Вот класс 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 . '.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); 
     } 
    }  
} 

Вот тестовый скрипт, то регистрационные данные для учетной записи одноразовой, так что не стесняйтесь проверить с ними:

$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; 

Вот что содержимое куки-файлы:

# 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 
+0

Вы знаете, что у них есть API, не так ли? Почему вы хотите получить доступ к eBay через cURL? – silkfire

+0

Я знаю API-интерфейс eBay, и я его очень хорошо знаю, мне нужно cURL в eBay выполнять действия, которые API не делает. –

ответ

2

Я был в состоянии понять это.

eBay использует довольно сложный метод для входа в систему. Это комбинация файлов cookie, скрытых полей и перенаправления javascript после успешного входа в систему.

Вот как я решил это.

Новый модифицированный класс:

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

Фактический код в использовании:

$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&amp;siteid=3&amp;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); 

Я использовал часть кода размещена here, благодаря drew010!

+0

вам лучше скрыть свои имя пользователя и пароль. – Umren

+0

Это разовая учетная запись, которую я сделал для этого, так что это действительно не имеет значения. –

+0

Кто-нибудь знает, работает ли это еще? – nmos

Смежные вопросы