获取“属性og”使用 PHP 从 URL 获取元标记
我想创建一个类似于 Facebook 使用的发布功能(您将链接粘贴到文本框中,点击帖子,它会发布标题、描述和图像)。我意识到最好提取具有 og 属性的元标记,例如“og:title”和“og:image”,因为如果我使用普通标记,有时它们会有换行符和其他类似的东西,并且会出现错误。
有没有办法使用 PHP 获取这些标签的内容,但无需 AJAX 或其他自定义解析器?出发点是:
<?php
$url = $_POST['link'];
?>
我们通过 POST 方法从上一页获取 URL,但是剩下的怎么办呢?
I want to create a posting feature similar to the one Facebook uses (You paste a link into textbox, hit post and it posts a title, description and an image). I realized that it is best to extract the meta tags that have og properties such as "og:title" and "og:image" because if I use normal tags, sometimes they have line breaks and such other things and it comes out with errors.
Is there a way to fetch contents of these tags using PHP, but without AJAX or other custom parsers? The starting point would be:
<?php
$url = $_POST['link'];
?>
We get the URL from the previous page through POST method, but how to do the rest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
解决方案是这样的:
The solution is this:
使用类似下面的内容:
在How上找到了这个通过 php 获取网页的开放图谱协议? - 搜索很有帮助,Google 也一样!
http://www.google.co.uk/search? q=元+属性+og+标签
Use something like the below:
Found this on How to get Open Graph Protocol of a webpage by php? - search is helpful, as is Google!
http://www.google.co.uk/search?q=meta+property+og+tags
使用这个: https://github.com/baj84/MetaData
它既简单又高效。
Use this: https://github.com/baj84/MetaData
It's easy and efficient.
我们通过 php(命令行实用程序)使用 Apache Tika,并使用 -j 表示 json :
http:// tika.apache.org/
这是来自随机监护人文章的输出示例:
We use Apache Tika via php (command line utility) with -j for json :
http://tika.apache.org/
This is a sample output from a random guardian article :
试试这个..
它对我有用..
try this..
it worked for me..