如何使用php curl获取有效的fb access_token?
我正在研究一个需要使用curl的用户访问令牌的项目。 用户将输入他的用户名和密码。他将使用curl登录Facebook。登录部分起作用。我还得到一个令牌,其中包含管道:xxxxxxx | xxxxxxxxxxxxxxxxx,但我想要一个访问令牌发布到登录用户墙。如何使用php curl获取有效的fb access_token?
,我使用的代码是:
// your facebook credentials
$username = "xxxxxxxxxxxxxxxx";
$password = "xxxxxxxx";
// access to facebook home page (to get the cookies)
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://www.facebook.com");
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_ENCODING, "");
curl_setopt ($curl, CURLOPT_COOKIEJAR, getcwd() . '/cookies_facebook.cookie');
curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
$curlData = curl_exec ($curl);
curl_close ($curl);
// do get some parameters for login to facebook
$charsetTest = substr ($curlData, strpos ($curlData, "name=\"charset_test\""));
$charsetTest = substr ($charsetTest, strpos ($charsetTest, "value=") + 7);
$charsetTest = substr ($charsetTest, 0, strpos ($charsetTest, "\""));
$locale = substr ($curlData, strpos ($curlData, "name=\"locale\""));
$locale = substr ($locale, strpos ($locale, "value=") + 7);
$locale = substr ($locale, 0, strpos ($locale, "\""));
$lsd = substr ($curlData, strpos ($curlData, "name=\"locale\""));
$lsd = substr ($lsd, strpos ($lsd, "value=") + 7);
$lsd = substr ($lsd, 0, strpos ($lsd, "\""));
// do login to facebook
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "https://login.facebook.com/login.php?login_attempt=1");
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($curl, CURLOPT_POSTFIELDS, "charset_test=" . $charsetTest . "&locale=" . $locale . "&non_com_login=&email=" . $username . "&pass=" . $password . "&charset_test=" . $charsetTest . "&lsd=" . $lsd);
curl_setopt ($curl, CURLOPT_ENCODING, "");
curl_setopt ($curl, CURLOPT_COOKIEFILE, getcwd() . '/cookies_facebook.cookie');
curl_setopt ($curl, CURLOPT_COOKIEJAR, getcwd() . '/cookies_facebook.cookie');
curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
$url = "https://graph.facebook.com/oauth/access_token";
$postString = "client_id=XXXXXXXX&client_secret=XXXXXXXXXXXXXXX&type=client_cred";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
$access_token = str_replace("access_token=", "", curl_exec($curl));
echo $access_token;
的SDK PHP库(你可以找到一个更新版本here)使用卷曲了。我们是否有机会重新发明轮子?
感谢您的答案。实际上我会将用户名和密码保存在数据库中,然后使用它们获取用户 – Saadi 2013-03-19 07:41:08
的访问令牌,这是不好的做法......您存储访问令牌,这对我的谦虚意见甚至太过分了。我永远不会将我的登录凭据给任何人 – tattvamasi 2013-03-19 07:44:01
你是对的,但该应用程序将自行托管在该人的网站上。这就是要求。 – Saadi 2013-03-19 08:00:12
PHP-SDK无论如何都使用curl ... – 2014-04-16 08:00:54