从Facebook页面URL中提取页面ID [英] Extracting page id from a facebook page url
问题描述
我有一个应用程序,当用户输入他们的Facebook粉丝页面网址时,我提取页面ID并将其存储到数据库中,以便以后在Facebook图形API中使用.
I have an application where when a user enters their facebook fan page url, I extract the page id and store that into the database to be used later in facebook graph api.
我添加了以下js来提取页面ID:
I have added the following js to extract page id:
<script>
var fburl= "https://www.facebook.com/audi";
if(fburl.indexOf("?") != -1) {
var fburl_new=fburl.split("/");
var fburl_newer=(fburl_new[fburl_new.length-1])
var fburl_newer =fburl_newer.split("?");
var fbnameonly=(fburl_newer[fburl_newer.length-2]);
} else {
var fbnameonly = fburl.substring(fburl.lastIndexOf('/') + 1);
}
if(fbnameonly==undefined) {
console.log(fburl);
}else {
console.log(fbnameonly);
}
</script>
因此,如果用户输入类似https://www.facebook.com/audi
的Facebook网址,则会得到输出audi
,这很好.
So, if I a user enters a facebook url like: https://www.facebook.com/audi
, I get the output audi
which is fine.
如果用户以https://www.facebook.com/pages/abc-def/1234568
的形式输入facebook网址,我会得到很好的输出1234568
.
If a user enters facebook url as : https://www.facebook.com/pages/abc-def/1234568
, I get the output 1234568
which is fine.
但适用于以下网址:
https://www.facebook.com/abc-def-12345678/
(应返回12345678)
https://www.facebook.com/abc-def-12345678/
(should return 12345678)
或
https://www.facebook.com/audi/timeline?ref=page_internal
(应返回audi)
我无法获取页面ID,有没有更好的方法可以从任何Facebook粉丝页面URL中提取页面ID并在Facebook图形api中使用它,例如:https://graph.facebook.com/page_id
动态地?
I am unable to get the page id, is there any better way by which I can extract page id from any facebook fan page url and use it in facebook graph api like: https://graph.facebook.com/page_id
dynamically ?
推荐答案
我想出了一种从任何Facebook页面网址获取页面ID的更好方法,但我不确定是否还有更好的选择,但这是编码我曾经使用过的代码:
I figured out a better way to get page id from any facebook page url, I am not sure if there is even better option to do this but here is the code what I have used:
$fbUrl = "https://www.facebook.com/Lo-stile-di-Anna-1521824524722026";
$graphUrl = "https://graph.facebook.com/?id=".$fbUrl."&access_token=xxxxx&fields=id";
$output = file_get_contents($graphUrl);
$output = json_decode($output, TRUE);
echo $output["id"]; // returns 1521824524722026
我尝试了各种Facebook网址,并且每次都能奏效,希望这对以后的人有所帮助.
I tried this with various facebook urls and it worked everytime, hope this helps someone in the future.
这篇关于从Facebook页面URL中提取页面ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!