[EMAIL PROTECTED] wrote:
> 
> 
> ----- Original Message -----
> From: "cosminx2003"
> 
> Hi, i want to login on yahoo with php cURL, i made a script but i
> can't make it work fine (it gives me blank page).
> Please tell me how can i fix it.
> Thanks
> 
> <?php
> 
> $username = "user";
> $password = "pass";
> $ch = curl_init();
> 
> curl_setopt ($ch, CURLOPT_URL, 'http://login.yahoo.com/config/login? 
> <http://login.yahoo.com/config/login?>');
> curl_setopt ($ch, CURLOPT_POST, 1);
> curl_setopt ($ch, CURLOPT_POSTFIELDS,
> "login=$username&passwd=$password&.src=&.tries=5&.bypass=&.partner=&.md5=&.hash=&.intl=us&.tries=1&.challenge=ydKtXwwZarNeRMeAufKa56.oJqaO&.u=dmvmk8p231bpr&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.v=0&.chkP=N&.last=&.done=");
> 
> curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookies.txt");
> curl_setopt ($ch, CURLOPT_COOKIEFILE, "cookies.txt");
> 
> curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; U; PPC
> Mac OS X;en) AppleWebKit/419.2.1 (KHTML, like Gecko) Safari/419.3');
> curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
> 
> $result=curl_exec ($ch);
> curl_close ($ch);
> echo $result;
> 
> ------------------------------------
> Hello,
> I have done this before but I used header information to control the 
> cookie without using cURL.
> 
> Some things that don't look right to me -
> 
> curl_setopt ($ch, CURLOPT_URL, 'http://login.yahoo.com/config/login? 
> <http://login.yahoo.com/config/login?>');
> 
> Shouldn't the '?' be with the rest of the query string instead here -
> "?login=$username&passwd=$password&.src=&......
> 
> and -
> 
> &.tries=5
> 
> did you copy this from a browser? will it work manually into a browser?
> 
> and
> 
> &.challenge=ydKtXwwZarNeRMeAufKa56.oJqaO
> 
> Is some sort of session token? Has it expired?

You're right, that is dynamic. I have written a function for scraping 
what input fields are currently on the page that will work perfectly for 
this.

Here is the script. I actually tested it with my username & password and 
it let me in just fine:

<?php

$cookiefile = 'cookies.txt';

$page = 
PostPage('http://login.yahoo.com/config/login','','',$cookiefile); 
//retrieve page without loging in.
$args = formflds($page); //get all of the input fields
$args['login'] = 'login'; // overwrite username
$args['passwd'] = 'passwd'; // overwrite password
$page = 
PostPage('http://login.yahoo.com/config/login',$args,'',$cookiefile); // 
login
echo $page;

###################################################################
function PostPage($URL, $args = array(), $referer = '', $cookiefile = '') {
   if ( $args == '' ) $args = array();
   if ( count($args) ) {
     foreach ( $args as $key => $val ) {
       $postfields .= "$key=" . urlencode($val) . "&";
     }
     $postfields = substr($postfields, 0, -1);
   }
   $ch = curl_init($URL);
   if ( !defined('CURL_NOHEADER') ) curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; U; PPC 
Mac OS X;en) AppleWebKit/419.2.1 (KHTML, like Gecko) Safari/419.3');
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       if ( defined('CURL_TIMEOUT') ) {
         curl_setopt($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT);
       }
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
   curl_setopt($ch, CURLOPT_ENCODING, 'fred');
   curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
   curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
   if ( $postfields ) curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
   if ( $referer ) {
     curl_setopt($ch, CURLOPT_REFERER, $referer);
     curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
   }
   if ( $cookiefile ) {
     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
   }
   if (!$webpage = curl_exec($ch)) $webpage = "Error: " . 
curl_errno($ch) . "\nDescription: " . curl_error($ch);
   curl_close($ch);
   return $webpage;
}

function formflds($page){
     preg_match_all("#<input[^>]*>#i",$page,$data);
     $result = array();
     $data = $data[0];
     foreach($data as $k => $v){
 
if(preg_match("#\bname\s*=\s*(['\"])?((?(1).*?|[^\s>]*))(?(1)\\1|[\s>])#is", 
$v, $name) ) {
 
preg_match("#\bvalue\s*=\s*(['\"])?((?(1).*?|[^\s>]*))(?(1)\\1|[\s>])#is", 
$v, $value);
           $result[$name[2]] = $value[2];
         }
     }
     preg_match_all("#<select[^>]*>.+?</select>#is",$page,$select);
     $select = $select[0];
     foreach($select as $k => $v){
 
preg_match("#\bname\s*=\s*(['\"])?((?(1).*?|[^\s>]*))(?(1)\\1|[\s>])#is",$v,$name);
       if(preg_match("#<option[^>]*selected[^>]*>#i",$v,$value));
       else $value[0] = $v;
 
preg_match("#\bvalue\s*=\s*(['\"])?((?(1).*?|[^\s>]*))(?(1)\\1|[\s>])#is",$value[0],$value);
       $result[$name[2]] = $value[2];
     }
   foreach ($result as $k => $v) if(is_null($v)) $result[$k] = '';
   return $result;
}

?>

Reply via email to