使用CURL和PHP登录时获取Facebook Cookie [英] Using CURL and PHP to get facebook cookie when login

查看:656
本文介绍了使用CURL和PHP登录时获取Facebook Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得my_cookie.txt与Facebook的Cookie,所以我可以重新使用它与CURL的其他Facebook页面。



这是我的PHP代码...当我尝试这个...我看到Facebook登录页面,但它给我一个错误,我的浏览器中的cookies必须是启用...但是启用。

  $ email ='my email'; 
$ password ='mypassword';
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,'https://login.facebook.com/login.php');
curl_setopt($ ch,CURLOPT_POSTFIELDS,'email ='。urlencode($ email)。'& pass ='。urlencode($ password));
curl_setopt($ ch,CURLOPT_POST,1);
curl_setopt($ ch,CURLOPT_HEADER,0);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ ch,CURLOPT_COOKIEJAR,my_cookies.txt);
curl_setopt($ ch,CURLOPT_COOKIEFILE,my_cookies.txt);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_USERAGENT,Mozilla / 5.0(Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3)Gecko / 20070309 Firefox / 2.0.0.3
$ page = curl_exec($ ch);

echo $ page;

这是我看到的错误





首先,这是违反facebook的TOS(更好地使用api)。



登录不打算登录登录页面。您必须首先浏览主页(在这种情况下使用curl)并将cookie存储在jar中。然后在登录期间使用cookie与您的登录请求。您可能需要从首页找到令牌或类似的东西(如果他们使用任何东西)。



所以这些步骤是为任何网站你想做一个登录请求:



1)使用curl浏览主页。使用jar存储cookie。



2)如果页面有任何标记或隐藏的参数,然后从html中解析。



3)初始化一个新的curl,使用user / pass +隐藏参数准备post参数。不要忘记添加cookie jar。


I'm trying to get my_cookie.txt with the facebook cookie so I can re-use it with CURL for other facebook pages.

Here is my PHP code... When I try this... i see the facebook login page but it gives me an error saying my cookies in my browser must be enable... BUT they are enable.

$email = 'my email';
$password = 'mypassword';
$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php');
    curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($email).'&pass='.urlencode($password));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
$page =      curl_exec($ch);

echo $page;

Here is the error I see

Any idea what I'm doing wrong?

解决方案

First of all it is against the TOS of facebook (better use api).

The login is just not about to hitting the login page. You have to browse the home page first (using curl in this case) and store the cookie in jar. Then during the login use the cookie with your login request. You may need to find the token or something like that from the home page (if they use any).

So the steps are for any site you want to make a login request:

1) Browse home page using curl. Store cookies using jar. And don't forget to close the curl.

2) If the page has any token or hidden parameter then parse that from the html.

3) Initialize a new curl, prepare post parameter with user/pass + hidden parameters. Don't forget to add cookie jar.

这篇关于使用CURL和PHP登录时获取Facebook Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆