检查 parse_url 中的子域
我正在尝试编写一个函数来从 Facebook 获取用户个人资料 ID 或用户名。他们将网址输入到表单中,然后我尝试判断它是 Facebook 个人资料页面还是其他页面。问题是,如果他们进入应用程序页面或具有子域的其他页面,我想忽略该请求。
现在我有:
$author_url = http://facebook.com/profile?id=12345;
if(preg_match("/facebook/i",$author_url)){
$parse_author_url = (parse_url($author_url));
$parse_author_url_q = $parse_author_url['query'];
if(preg_match('/id[=]([0-9]*)/', $parse_author_url_q, $match)){
$fb_id = "/".$match[1];}
else{ $fb_id = $parse_author_url['path'];
}
$grav_url= "http://graph.facebook.com".$fb_id."/picture?type=square";
}
echo $gav_url;
如果 $author_url
有“id=”,则可以使用它作为配置文件 ID,如果没有,则它必须是用户名或页面名称,因此请使用它。我需要再运行一次检查,看看该 url 是否包含 facebook 但属于子域,请忽略它。我相信我可以在第一个 preg_match 中做到这一点 preg_match("/facebook/i",$author_url)
谢谢!
I am trying to write a function to just get the users profile id or username from Facebook. They enter there url into a form then I'm trying to figure out if it's a Facebook profile page or other page. The problem is that if they enter an app page or other page that has a subdomain I would like to ignore that request.
Right now I have:
$author_url = http://facebook.com/profile?id=12345;
if(preg_match("/facebook/i",$author_url)){
$parse_author_url = (parse_url($author_url));
$parse_author_url_q = $parse_author_url['query'];
if(preg_match('/id[=]([0-9]*)/', $parse_author_url_q, $match)){
$fb_id = "/".$match[1];}
else{ $fb_id = $parse_author_url['path'];
}
$grav_url= "http://graph.facebook.com".$fb_id."/picture?type=square";
}
echo $gav_url;
This works if $author_url
has "id=" then use that as the profile id if not then it must be a user name or page name so use that instead. I need to run one more check that if the url contains facebook but is a subdomain ignore it. I belive I can do that in the first preg_match preg_match("/facebook/i",$author_url)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要忽略 facebook 子域,您可以确保它
是
facebook.com
。如果是其他类似
login.facebook.com
或apps.facebook.com
的内容,则无需继续。或者,您还可以确保 URL 以
http://facebook.com
开头,如下所示:To ignore facebook subdomains you can ensure that
is
facebook.com
.If its anything else like
login.facebook.com
orapps.facebook.com
you need not proceed.Alternatively you can also ensure that the URL begins with
http://facebook.com
as:这不是您所要求的直接解决方案,但这些部件可以完成您需要做的事情。
我发现子域导致 parse_url 出现问题。也就是说,它返回一个仅包含 $result['path'] 且没有 'host' 或 'scheme' 的数组。
我的理论是,如果 parse_url 没有 'host' 或 'scheme' 结果,并且它在字符串中具有域后缀 ( .ext ),那么它就是一个子域。
这是代码:
( $src 是我必须从子域中整理出相对 src 的 url ):
可以通过解析第一个“/”之前的 subdomain==true 字符串并使用正则表达式测试字符来编写进一步的确认。
希望这可以帮助一些人。
This isn't a direct solution for what you were asking but the parts are here to do what you need to do.
I found that a subdomain resulted in an issue with parse_url. Namely it returned an array with only $result['path'] and no 'host' or 'scheme'.
My theory here is if there is no 'host' or 'scheme' results from parse_url and it has domain suffix ( .ext ) in the string, it is a subdomain.
Here is the code:
(the $src is a url I had to sort out the relative src from subdomains ):
Could write a further confirmation by parsing the subdomain==true strings before the first '/' and testing against characters with a RegEx.
Hope this helps some people out.