Home  >  Article  >  Backend Development  >  Use Facebook's SDK to determine whether the visitor has clicked and become a member of this site's fan group_PHP Tutorial

Use Facebook's SDK to determine whether the visitor has clicked and become a member of this site's fan group_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:36:16873browse

There are activities going on in the company today, and one of the activities requires visitors to click on the Facebook fan group, and after clicking the praise, they will be given cash. Everyone knows that Facebook is blocked. After searching on Baidu, I found that because Due to the wall, very few people in China are involved in Facebook development.

Facebook’s API is said to be simple. It’s really simple. You only need to use an iframe to put the html code. But this time because cash is involved, I don’t want to use curl to grab the source code to make fuzzy judgments. That is very inaccurate, and there are very few in China. There is content about Facebook, and even if it exists, it is mainly news. There are occasionally a few technical articles that are old stuff from a few years ago. Now I can’t use the code when I get it. I have no choice but to chew on it all night. English API manual. Speaking of manuals, by the way, the easiest one to find when we read the manual is https://developers.facebook.com/docs/reference/php. This description provides many built-in methods for us to call directly. The required content, but because it is preset by Facebook, the method provided cannot meet very special needs (just like the function I want to implement today is very special), I finally found https://developers.facebook.com/docs /reference /fql/, here we mainly talk about Facebook’s FQL (this thing is very powerful, as long as you can think of almost all the functions you can think of, this example was solved thanks to the use of FQL)

Before throwing away the code, let me first briefly mention that if you want to develop Facebook programs, you must apply for its appid and secret. The application method is very simple from https://developers.facebook.com/apps/?action =create application is enough, and I won’t go into details due to space limitations (if you find it difficult, you can join my QQ group 223494678 for discussion). After you have these two things, you still need to download Facebook’s SDK. The download address is https: //github.com/facebook/facebook-php-sdk, unzip it after downloading, and only take out the src folder. It is enough. You can delete everything else. The code is below. I have added comments line by line

<?<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>

I hope this article can serve as a starting point

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/739852.htmlTechArticleThere are activities going on in the company today. One of the activities requires visitors to click on the Facebook fan group. You will be given cash after clicking 讃. It is well known that Facebook has been blocked. I searched it on Baidu...
Statement:
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