duanmaifu3428 2012-06-09 03:10
浏览 110

PHP处理URL中的片段标识符

I've got quite a dilemma here and would like some suggestions from the community. Here's my problem:

I've created a website that has lots of dynamic content, all content is pulled via AJAX and displayed via JS. The way I'm currently allowing direct links to my content is by modifying the fragment identifier on the fly with JS, here's an example:

http://www.mysite.com/home#/123

Where 123 is the ID of the content. When the content is closed it strips the fragment identifier off again.

This works wonderfully, and when people load the page my AJAX request is successfully requesting the '123' article. But I've just implemented a Facebook like button on all the articles, and now I've hit a brick wall.

Basically, the way Facebook finds what thumbnail and titles and URL to associate with your 'liked' content is to check the meta tags of the URL passed into the fb like iframe. It does this by doing a GET request at the URL, checking the content of the meta tags, and then displaying those on Facebook.

The problem is that fragment identifiers are not passed to the server, so I have no way in PHP (that I'm aware of) to dynamically generate these meta tags with the content from the specifically requested article.

For example, I wanted to be able to do something like this:

$id_vals = get_id_values($hash_fragment);
echo '<meta property="og:title" content="'.$id_vals['title'].'" />';

Your first thought is probably: Why not use a GET request or query string? i.e.:

http://www.mysite.com/home?id=123

However the problem with this approach is that Javascript cannot dynamically change this portion of a URL, it can only change the fragment identifier. This means that all direct links to our articles will require page refreshes. This goes against our whole 'dynamic' approach we have going - which I might add is really nice :)

So I'm kind of stumped at the moment. The only semi-solution I can think of is to use an approach of both:

In the fb like requests use get parameters and for direct links use fragment identifier.

But this poses the following problems:

  • If a direct link is pasted on Facebook from a user it will include the fragment identifier, and the correct meta data won't display on Facebook.
  • I would need to do some weird redirection magic to fix the format of the URL to remove the ID (i.e. detect if GET parameter is passed in and redirect to the fragment identifier equivalent). But I don't know if FB would also follow the redirect and fetch the meta tags from the redirected page instead of the first page, which would effectively make this solution useless anyway.

Sorry for the wall of text! Appreciate any input you can provide.

EDIT: I just tested my proposed solution involving a combination of both, and I can confirm Facebook follows the redirect, so this solution doesn't work at all. This is depressing!

EDIT EDIT: One possible way to implement my 'mix' idea is to use a window.location redirect instead of a PHP header() call redirect, so Facebook fetches the meta tags from the original page, for those of you with a similar problem.

Thanks.

  • 写回答

3条回答 默认 最新

  • duanfengshang1088 2012-06-09 04:52
    关注

    PHP's parse_url function returns everything including the hash fragment, so you could either use parse_url and access the fragment key of the returned array

    $url = 'http://www.mysite.com/home#/123';
    $parts = parse_url($url);
    $fragment = $parts['fragment']; // => '/123'
    

    or alternatively pass in the component flag PHP_URL_FRAGMENT, and just retrieve the fragment itself

    $fragment = parse_url($url, PHP_URL_FRAGMENT); // => '/123'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?