LoveCats Posted June 18, 2011 Share Posted June 18, 2011 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<mpl=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<mpl=sso&hl=en_US<mpl=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<mpl=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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/239702-phpcurl-problem-login-is-successful-yet-unable-to-browse-members-area/ Share on other sites More sharing options...
pornophobic Posted June 18, 2011 Share Posted June 18, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239702-phpcurl-problem-login-is-successful-yet-unable-to-browse-members-area/#findComment-1231383 Share on other sites More sharing options...
LoveCats Posted June 18, 2011 Author Share Posted June 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239702-phpcurl-problem-login-is-successful-yet-unable-to-browse-members-area/#findComment-1231396 Share on other sites More sharing options...
LoveCats Posted June 26, 2011 Author Share Posted June 26, 2011 No one could help? Please at least show me some direction. Quote Link to comment https://forums.phpfreaks.com/topic/239702-phpcurl-problem-login-is-successful-yet-unable-to-browse-members-area/#findComment-1234861 Share on other sites More sharing options...
Skamrani2002 Posted June 5, 2012 Share Posted June 5, 2012 hey im still getting the error that is your cookies is off trun it on...? Can any body help me Quote Link to comment https://forums.phpfreaks.com/topic/239702-phpcurl-problem-login-is-successful-yet-unable-to-browse-members-area/#findComment-1351314 Share on other sites More sharing options...
kicken Posted June 5, 2012 Share Posted June 5, 2012 Use youtube's Data API rather than try and hack together a horrible screen-scrape solution. Quote Link to comment https://forums.phpfreaks.com/topic/239702-phpcurl-problem-login-is-successful-yet-unable-to-browse-members-area/#findComment-1351319 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.