Jump to content

PHP/cURL Problem - Login is successful, yet unable to browse members area.


LoveCats

Recommended Posts

Hello,

 

I was just trying to create a php script to login to video and get get some members data like, recently favorited video, active no. of subscriptions etc.

 

This is where I am running into trouble, I've created a code for logging in. The login is successful, the cookie is also saved. I am logging in with persistent cookie so its doesn't expire. But after that, if I try to get to "http://youtube.com" with the same cookie, it doesn't work. The page returned is same as the non-logged in one.

 

Please can anyone help me regarding the issue. I would be greatly thankful to you.

 

And please don't ask me to use the YouTube API. I am planning to add certian functionality which the API don't support. So even if my current probelm [getting active number of susbcriptions etc] is solved, I'll require a direct intervention in near future when I'll implement the other functionality which is not a part of API. So its better to make a custom code from starting.

 

 

Here's my code:

 

<?php
$username = 'USRENAME';
$password = 'PASSWORD';
$cookie_jar = "cookies/cookie.txt";
$useragent = "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";

$login_form = LoginPage($cookie_jar, $useragent); 
$final = Login($login_form, $username, $password, $cookie_jar, $useragent);
$video = getVideo("http://www.youtube.com/watch?v=PM2NocuEihw", $cookie_jar, $useragent);

print_r($video);

function Login($form, $username, $password, $cookie_jar, $agent) {

//$referer = "https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true&continue=http://www.youtube.com/signin?action_handle_signin=true&nomobiletemp=1&hl=en_US&next=%2Findex&hl=en_US&ltmpl=sso";

$pattern = '/(?s)name="GALX"(.*?)value="(.*?)"/';
preg_match($pattern, $youtube_form, $matches); 
$galx = $matches[2];

$post = "ltmpl=sso&continue=";
$post .= urlencode("http://www.youtube.com/signin?action_handle_signin=true&nomobiletemp=1&hl=en_US&next=/");
$post .= "index&service=youtube&uilel=3&ltmpl=sso&hl=en_US&ltmpl=sso&GALX=$galx&Email=$username&Passwd=$password&PersistentCookie=yes&rmShown=1&signIn=Sign in&asts=";

       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL,"https://www.google.com/accounts/ServiceLoginAuth?service=youtube");
       curl_setopt ($ch, CURLOPT_USERAGENT, $agent);
       curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
       curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
       curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
       curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt ($ch, CURLOPT_POSTFIELDS, $post); 
       curl_setopt ($ch, CURLOPT_POST, 1); 
       curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
        
       $output = curl_exec($ch);
        curl_close($ch);
        
        return $output;
}

function LoginPage($cookie_jar, $agent) {

$url = "https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true&continue=http://www.youtube.com/signin?action_handle_signin=true&nomobiletemp=1&hl=en_US&next=%2Findex&hl=en_US&ltmpl=sso";
$referer = "http://www.youtube.com/";
           
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_REFERER, $referer); 
       curl_setopt ($ch, CURLOPT_URL, $url);
       curl_setopt ($ch, CURLOPT_USERAGENT, $agent);
       curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
       curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
       curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 30);

        $output = curl_exec($ch);
        curl_close($ch);
        
        $pattern = '/(?s)\<form(.*?)\<\/form\>/';
        preg_match_all($pattern,  $output , $matches);
        
        $youtube_form = $matches[0][1];
       
        return $youtube_form;

}

function getVideo($url, $cookie_jar, $agent){
       $referer = "";
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_REFERER, $referer); 
       curl_setopt ($ch, CURLOPT_URL, $url);
       curl_setopt ($ch, CURLOPT_USERAGENT, $agent);
       curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
       curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 30);

        $output = curl_exec($ch);
        curl_close($ch);
       
        return $output;
}

?>

Link to comment
Share on other sites

Do you have error reporting on?

 

Notice: Undefined offset: 1 in C:\xampp\htdocs\youtube.php on line 66

Notice: Undefined variable: youtube_form in C:\xampp\htdocs\youtube.php on line 18

Notice: Undefined offset: 2 in C:\xampp\htdocs\youtube.php on line 19

Link to comment
Share on other sites

Do you have error reporting on?

 

Notice: Undefined offset: 1 in C:\xampp\htdocs\youtube.php on line 66

Notice: Undefined variable: youtube_form in C:\xampp\htdocs\youtube.php on line 18

Notice: Undefined offset: 2 in C:\xampp\htdocs\youtube.php on line 19

 

The second error is just mistake while experimenting. I just written $youtube_form there instead of $form. While I tested things were proper. However, the error you've notified doesn't seem to relate to my question. Its saying undefined offset because I haven't specifically allotted value to matches[0][1] before using it I suppose.

 

As I've stated, the cookies are being stored but while using it with getVideo() function, those cookies are just not working. The login is successful.

Link to comment
Share on other sites

  • 11 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.