远程登录到Facebook帐户 [英] Remote login to facebook account

查看:235
本文介绍了远程登录到Facebook帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了我的项目目的,我需要远程登录我的Facebook帐户,并从那里检索一些信息。对于登录目的,我使用PHP的cURL库。在执行代码,Facebook页面要求我启用我已经启用的浏览器的Cookie。代码有问题吗?任何人都可以帮助我远程登录我的帐户吗?我是一个新手,如果有任何帮助真的会感激。
这是代码

For my project purpose, i need to login to my facebook account remotely and retrieve some information from there. For the login purpose, i am using cURL library of PHP. On execution of the code, facebook page asks me to enable the cookies on my browser which i have already enabled. Is there any problem with the code? Can anybody help me out in logging into my account remotely? I am a newbie and would really appreciate if any help comes. Here is the code

<?php

 //create array of data to be posted

 $post_data['email'] = '*********';

 $post_data['password'] = '**********';

 $post_data['action'] = 'login';


 //traverse array and prepare data for posting 

 foreach ( $post_data as $key => $value) {

  $post_items[] = $key . '=' . $value;

 }

 //create the final string to be posted using implode()

 $post_string = implode ('&', $post_items);



 //create cURL connection

 $ch = curl_init('https://login.facebook.com/login.php?');



 //set options

 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

 curl_setopt($ch, CURLOPT_USERAGENT, 
   "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);



 //set data to be posted

 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);



 //perform our request

 $result = curl_exec($ch);



 print_r($result);



 //close the connection

 curl_close($ch);

 ?>






推荐答案

要使用带curl的cookie,您需要指定一个cookie jar,这是应存储和加载cookie的位置。您可以使用 CURLOPT_COOKIEFILE CURLOPT_COOKIEJAR 选项执行此操作。这样的东西应该这样:

To use cookies with curl you'll need to specify a "cookie jar", which is where the cookies should be stored and loaded from. You can do this using the CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR options. Something like this should do it:

curl_setopt($ch, CURLOPT_COOKIEJAR, 'facebook_cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'facebook_cookies.txt');

查看 http://php.net/curl_setopt 了解更多信息。

另外,您也可以传递 $ post_fields 作为 CURLOPT_POSTFIELDS 的数组,因此您实际上不必自己生成查询字符串。如果你想这样做,还有一个 http_build_query()函数。这与您发布的问题无关。

As an aside, you can also pass $post_fields as an array to CURLOPT_POSTFIELDS, so you don't actually have to generate the query string yourself. If you want to do that, there is also a http_build_query() function that does this. This is unrelated to the issue you posted though.

这篇关于远程登录到Facebook帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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