2009-10-16 9 views
1

Salut à tous. J'ai cherché pendant quelque temps quelque chose qui pourrait m'aider avec ça. J'écris un script qui va se connecter à Gamefly.com et ajouter 50 jeux à ma file d'attente. La partie de la file d'attente fonctionne assez bien, mais la connexion ne fonctionnera tout simplement pas. J'ai lu sur l'utilisation de cURL avant, et j'ai remarqué que PHP a assez de temps avec cela. Donc, voici ce que j'ai trouvé:Connexion automatique au site Web en utilisant cURL

$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$[email protected]xxxx.com&ctl00$ctl00$ctl00$MainContent$MainContent$MainContent$LoginForm$password=xxxx'); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt"); 
$test = curl_exec($ch); 

Évidemment, je pense que je reçois les mauvais champs quand je fais cela. Est-ce que quelqu'un a des idées sur comment je peux l'obtenir pour me connecter, ou suis-je désossé? Merci.

+0

Cela dépend vraiment du site, pourquoi auriez-vous besoin de le faire, si c'est une raison valable (NON spam) vérifier avec les développeurs de gamefly je suis sûr qu'ils vous donneront une API à utiliser – RC1140

+0

GameFly n'a pas d'API. J'ai demandé et plaidé dans le passé. C'est une application personnelle que j'utilise pour déterminer quels jeux je veux dans ma file d'attente en fonction de critères. –

Répondre

4

Essayez ceci:

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

Comment appeler:

// 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$[email protected]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 
+0

J'ai modifié mon code pour utiliser ce que vous avez écrit, mais cela ne fonctionne pas. Je suis assez sûr à ce stade, que j'ai des champs incorrects lorsque je soumets le formulaire ou quelque chose d'autre de ce genre. Je vais voir si je peux comprendre ça. –

Questions connexes