今天公司裡要做活動,其中有一項活動內容是要求來訪者按一下facebook粉絲團的讃,按了讃之後贈送現金.Facebook被墻大家眾所周知,在百度搜了一下發現因為被墻的原因導致國內涉及到Facebook開發的極少.
Facebook的API說簡單確實很簡單,只需要用iframe放html代碼就行了,但是這次因為涉及到了現金,我不想用curl抓取源碼模糊判斷,那樣很不准確,加上國內極少有關於Facebook的內容,就算是有也主要是新聞一類的,偶爾有幾篇技術文章也是幾年前老掉牙的玩意,現在拿過代碼來根本就不能用,迫於無奈只好啃了一晚上的英文版API手冊,說到手冊順便提一下我們看手冊的時候最容易找到的就是https://developers.facebook.com/docs/reference/php,這個說明中提供了很多內置的方法讓我們直接調用需要的內容,但是因為是facebook預設的因此提供的方法無法滿足很特殊的需求(就像今天我要實現的功能就非常特殊),最終我找到了https://developers.facebook.com/docs/reference/fql/,這裡主要講述facebook的FQL(這玩意很強大,只要你能想到的功能幾乎都能實現,本例就是得益于使用FQL才得以解決)
在丟代碼之前我首先簡單的提一下,要想開發facebook的程序就必須要申請到他的appid和secret,申請方法很簡單從https://developers.facebook.com/apps/?action=create申請就行了,礙於篇幅不再贅述(如果感覺有難度的話可以進我的QQ群223494678討論).有了這兩個玩意之後你還需要下載facebook的SDK才行,下載地址是https://github.com/facebook/facebook-php-sdk,下載之後解壓縮,只拿出src文件夾來就足夠用了,別的都可以刪掉,下邊是代碼,我有逐行加入註釋
<?<span PHP </span><span //</span><span 調用函數把結果賦值給變數</span> <span $a</span>=is_Like('213202422194942'<span ); </span><span if</span>(<span $a</span>[0]===<span TRUE</span><span ) { </span><span echo</span> '已經按讃'<span ; }</span><span else</span><span { </span><span echo</span> <span $a</span>[1<span ]; } </span><span unset</span>(<span $a</span>); <span //</span><span 釋放變數</span> <span die</span><span (); </span><span /*</span><span * 判斷用戶是否已經按讃(粉絲團) 參數: $pid:int型,粉絲團的id,這個值可以用firebug抓得到,如果不會可以進我的QQ群223494678討論 返回: array( 0=>bool型,[已經按讃則TRUE;反之FALSE] 1=>str型,當[0]=FALSE時的提示內容 ) 用法: $a=is_Like('213202422194942'); if($a[0]===TRUE) { echo '已經按讃'; }else{ echo $a[1]; } /*</span><span */</span> <span function</span> is_Like(<span $pid</span><span ){ </span><span if</span>(<span trim</span>(<span $pid</span>)=='') {<span return</span> <span array</span>(<span FALSE</span>,'粉絲團的ID不可為空!'<span );} </span><span //</span><span 調用SDK</span> <span require</span>('src/facebook.php'<span ); </span><span //</span><span 實例化對象</span> <span $FB</span>=<span new</span> Facebook(<span array</span><span ( </span>'appId' => '填寫你申請到的內容', 'secret' => '填寫你申請到的內容', 'cookie' => <span true</span>,<span )); </span><span //</span><span 如果對象建立失敗</span> <span if</span>(!<span is_object</span>(<span $FB</span><span )) { </span><span unset</span>(<span $FB</span><span ); </span><span return</span> <span array</span>(<span FALSE</span>,'加載Facebook的API失敗!'<span ); } </span><span //</span><span 提取當前來訪者id</span> <span $uid</span> = <span $FB</span>-><span getUser(); </span><span if</span>(!<span $uid</span>) { <span //</span><span 如果id提取失敗,則要求用戶登錄</span> <span $r</span>=<span array</span>(<span FALSE</span>,'<a href="'.<span $FB</span>->getLoginUrl(<span array</span>('scope'=>'publish_stream')).'">點擊登入</a>'<span ); }</span><span else</span>{ <span //</span><span 如果id提取成功 //用FQL查詢該用戶在page_fan表內是否有關注id=213202422194942的粉絲頁</span> <span $fql</span>=<span $FB</span>->api(<span array</span>('method' => 'fql.query','query' => 'SELECT page_id from page_fan where page_id="'.<span $pid</span>.'" and uid='.<span $uid</span>.''<span )); </span><span if</span>(<span is_array</span>(<span $fql</span>) and <span isset</span>(<span $fql</span>[0]['page_id']) and <span trim</span>(<span $fql</span>[0]['page_id'])!=''<span ) { </span><span $r</span>=<span array</span>(<span TRUE</span>,''<span ); }</span><span else</span><span { </span><span $r</span>=<span array</span>(<span FALSE</span>,'尚未按讃!'<span ); } } </span><span //</span><span 釋放變數</span> <span unset</span>(<span $pid</span>,<span $fql</span>,<span $uid</span>,<span $FB</span><span ); </span><span return</span> <span $r</span><span ; }</span>
希望此文能起到拋磚引玉的作用