Home > Backend Development > PHP Tutorial > yii开发, Facebook api 互联登录,请问流程

yii开发, Facebook api 互联登录,请问流程

WBOY
Release: 2016-06-13 12:05:13
Original
899 people have browsed it

yii开发, Facebook api 互联登录,请教流程
yii开发, Facebook api 互联登录,请教一下具体流程?
------解决方案--------------------
facebook session获取 sdk会做,不用自己写。

sdk应该包含以下三个文件
base_facebook.php
facebook.php
fb_ca_chain_bundle.crt

<br /><?php<br />include "facebook.php";<br />$oFacebook = new Facebook(array(<br />				'appId'			=>	'xxx',<br />				'secret'		=>	'xxx',<br />				'cookie'		=>	true,<br />				'fileUpload'	=>	true<br />			));<br /><br />$user = $oFacebook->getUser();<br />if($user){<br />	try{<br />		$user_profile = $oFacebook->api('/'.$user);	//判斷是否獲取到用戶信息<br />	} catch (FacebookApiException $e){<br />		$user = null;<br />	}<br />	if($user){<br />		return $user;	//返回用戶id<br />	}<br />}else{<br />	$loginUrl = $oFacebook->getLoginUrl(array(<br />							'scope'			=>	'xxxx', // 这里填权限,要获取的东西 例如 publish_stream,user_photos,email,offline_access,user_birthday,user_notes<br />							'redirect_uri'	=>	'xxx'   // 这里填在facebook login后要回跳的地址<br />						));<br />	return $loginUrl;<br />}<br />?><br />
Copy after login


得到$loginUrl后,跳转到这个地址
当facebook 登入成功后,会返回到 redirect_uri这个地址,并把参数带回

redirect_uri php可以这样写
<br /><?php<br />  $userid = $oFacebook->getUser();<br /><br />  $facebook_accesstoken = $facebook->getAccessToken();<br />  echo $facebook_accesstoken.'<br>';<br /><br />	try{<br />		$param = array(<br />			'method'		=>	'fql.query',<br />			'query'			=>	"SELECT 'uid,name,pic,pic_square,email,sex,birthday_date' FROM user WHERE uid='".$userid."'",<br />			'access_token'	=>	$facebook_accesstoken<br />		);<br />		$result = oFacebook->api($param);<br />	} catch(FacebookApiException $e){<br />		return array();<br />	}<br /><br />	if($result){<br />		$user_profile = array();<br />		foreach($result[0] as $key=>$val){<br />			$user_profile[$key] = $val;<br />		}<br />		return $user_profile;<br />	}else{<br />		return array();<br />	}		<br />	print_r($result);<br />?><br />
Copy after login


剩下的就是自己服务器那边的操作了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template