3
    <?php
$email = "";
$password = "";
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";


//curl get
function curl_get($url, $cookiefile) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

//curl post

function curl_post($url, $cookiefile, $post) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

//cookie file

$cookiefile = "cookie.txt";


//get url to grab GALX & dsh to login
$data = curl_get("https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true&continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&hl=en_US&ltmpl=sso", $cookiefile);


preg_match('/name="GALX"\s*value="(.*?)"/', $data, $galx);

preg_match('/name="dsh" id="dsh"\s*value="(.*?)"/', $data, $dsh);


//login
$data = curl_post("https://www.google.com/accounts/ServiceLoginAuth", $cookiefile, "ltmpl=sso&continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&service=youtube&uilel=3&dsh=$dsh[1]&ltmpl=sso&hl=en_US&ltmpl=sso&timeStmp=&secTok=&GALX=$galx[1]&Email=$email&Passwd=$password&PersistentCookie=yes&rmShown=1&signIn=Sign+in&asts=");

//auth url
$data = curl_get("https://www.google.com/accounts/CheckCookie?continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&hl=en_US&service=youtube&ltmpl=sso&chtml=LoginDoneHtml", $cookiefile);


// youtube 
$data = curl_get("http://www.youtube.com/", $cookiefile);
print $data;

?>

here's my code, on the last line when i print out the contents of youtube.com it shows i'm not logged in.. anyone know what i'm doing wrong here? to login to google you need to grab the GALX and dsh values and i'm doing so. this is such a headache!

1
  • Didn't you ask this question earlier tonight? Commented Feb 4, 2011 at 8:11

1 Answer 1

4

here's my code, on the last line when i print out the contents of youtube.com it shows i'm not logged in..

Google is right :) You see, you curl_init( ) over and over, which means you start new sessions every call to your curl_get and curl_post functions. You should only init once and pass along the resource you retrieve. That should solve your problem.

EDIT: Took the liberty of rewriting your code to illustrate:

<?php
$email = "";
$password = "";



//curl get
function curl_get( $curl, $url, $cookiefile) {
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1" );
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($curl);
    return $data;
}

//curl post

function curl_post( $curl, $url, $cookiefile, $post) {
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1" );
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($curl);
    return $data;
}

//cookie file
$cookiefile = "cookie.txt";

$curl = curl_init( );

//get url to grab GALX & dsh to login
$data = curl_get( $curl, "https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true&continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&hl=en_US&ltmpl=sso", $cookiefile);


preg_match('/name="GALX"\s*value="(.*?)"/', $data, $galx);

preg_match('/name="dsh" id="dsh"\s*value="(.*?)"/', $data, $dsh);


//login
$data = curl_post( $curl, "https://www.google.com/accounts/ServiceLoginAuth", $cookiefile, "ltmpl=sso&continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&service=youtube&uilel=3&dsh=$dsh[1]&ltmpl=sso&hl=en_US&ltmpl=sso&timeStmp=&secTok=&GALX=$galx[1]&Email=$email&Passwd=$password&PersistentCookie=yes&rmShown=1&signIn=Sign+in&asts=");

//auth url
$data = curl_get( $curl, "https://www.google.com/accounts/CheckCookie?continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&hl=en_US&service=youtube&ltmpl=sso&chtml=LoginDoneHtml", $cookiefile);


// youtube 
$data = curl_get( $curl, "http://www.youtube.com/", $cookiefile);
print $data;
Sign up to request clarification or add additional context in comments.

5 Comments

hi thanks for your response! i tried to run the code and i keep getting Warning: curl_setopt(): 2 is not a valid cURL handle resource on each line
Because the curl handle is being closed in each of the functions and shouldn't be closed until the end.
@joey Sorry, never noticed your response, but Dominic Watson is right; the cURL session is still closed in the functions. I've edited my answer, better late than never, right? (-:
@Berry Langerak, this one still does not work... it still says "Your browser's cookie functionality is turned off. Please turn it on" on auth step...
@YasinErgul The cookies are enabled though, because of the COOKIEFILE and COOKIEJAR options. Try running the script and look at the contents of cookie.txt, to see if there's anything in there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.