Home >Backend Development >PHP Tutorial >Example tutorial of adding Facebook comment plug-in to the page of PHP site, facebook example tutorial_PHP tutorial
First, you need to create an APP on facebook. For the creation method, see https://developers. facebook.com/, the APP has an item to fill in the Domain, fill in the Domain of your website here. (The APP is bound to the domain and cannot be filled in randomly)
Then you can use facebook comments plugins.
Using facebook comments plugins, facebook comments can be inserted into the page.
Generate code method: https://developers.facebook.com/docs/plugins/comments
For example: There is a page called http://www.example.com/. Insert the following code into this page to use comments plugings.
<!-- include facebook js sdk --> <script id="facebook-jssdk" src="//connect.facebook.net/en_GB/all.js#xfbml=1&appId=这里填写APPID"></script> <!-- comments plugins --> <fb:comments colorscheme="light" numposts="4" height="360px;" width="614px" href="http://www.example.com/" fb-xfbml-state="rendered" class="fb_iframe_widget"></fb:comments>
Displayed as follows on the page
Read the total number of shares and comments on the page
https://graph.facebook.com/?ids={YOUR_URL}
{YOUR_URL} requires urlencode
For example: https://graph.facebook.com/?ids=http://www.example.com/
Return:
{ "http://www.example.com/": { "id": "http://www.example.com/", "shares": 399517, "comments": 392 } }
The code is as follows:
<?php $url = 'http://www.example.com/'; $api = 'https://graph.facebook.com/?ids='; $result = json_decode(file_get_contents($api.urlencode($url)), true); print_r($result); ?>
Read page comment list
https://graph.facebook.com/comments/?ids={YOUR_URL}
{YOUR_URL} requires urlencode
For example: https://graph.facebook.com/comments/?ids=http://www.example.com/
Return:
{ "http://www.example.com/": { "comments": { "data": [ { "id": "395320319544_27462154", "from": { "id": "100000223906701", "name": "Thu\u1eadn Phan Thanh" }, "message": "hello moto", "can_remove": false, "created_time": "2013-10-07T10:01:40+0000", "like_count": 1, "user_likes": false }, { "id": "395320319544_27877980", "from": { "id": "100001638736612", "name": "L\u00e3 Minh" }, "message": "hi you", "can_remove": false, "created_time": "2013-11-13T02:57:01+0000", "like_count": 4, "user_likes": false }, { "id": "395320319544_27879381", "from": { "id": "100004229015145", "name": "Th\u00f9y Dung" }, "message": "Mg \u1ee7ng h\u1ed9 t\u1edb v\u1edbi nh\u1edb \u003C3", "can_remove": false, "created_time": "2013-11-13T05:38:12+0000", "like_count": 3, "user_likes": false } ... ], "paging": { "cursors": { "after": "MjU0", "before": "Mzk4" }, "next": "https://graph.facebook.com/v1.0/395320319544/comments?limit=25&after=MjU0" } } } }
Request according to the next url to get the comment content of the next page
The code is as follows:
<?php $url = 'http://www.example.com/'; $api = 'https://graph.facebook.com/comments/?ids='; $result = json_decode(file_get_contents($api.urlencode($url)), true); print_r($result); ?>