简体   繁体   English

如何从PHP中的推文文本中获取图像URL?

[英]How to get image url from tweet text in php?

I'm currently displaying pics in my app from twitter's default image service since they get included in the json response but I'd like to try to get images from yfrog, twitpic, lockerz or similar providers. 我目前正在我的应用程序中显示来自twitter的默认图像服务的图片,因为它们包含在json响应中,但我想尝试从yfrog,twitpic,lockerz或类似的提供者获取图像。

I'm using the rest api so I was thinking about adding filter:links to the search query, extract the url from the tweet and check if the link is an image but I'm not sure exactly how to get the url since I assume it'll require some regular expressions plus most of the tweets url are shortened versions that redirect to the actual photo somewhere so I believe this could be a problem. 我正在使用其余的api所以我正在考虑添加filter:links到搜索查询,从推文中提取url并检查链接是否是图像,但我不确定如何获取网址,因为我假设它需要一些正则表达式以及大多数推文网址是缩短版本,重定向到某个地方的实际照片所以我相信这可能是一个问题。 It'd be nice if I could verify that the url contains any of the image providers mentioned above too (kind of like a first filter before checking if the url is an image) 如果我能够验证url是否包含上面提到的任何图像提供程序(在检查url是否为图像之前有点像第一个过滤器),那就太好了

Could someone point me in the right direction? 有人能指出我正确的方向吗? Thanks in advance! 提前致谢!

For detecting the links, just google for a regex to match a url. 要检测链接,只需谷歌搜索正则表达式以匹配网址。 Like this here: 像这样:
http://snipplr.com/view/2371/ or http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-developers http://snipplr.com/view/2371/http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-developers
and cycle through the matches array: http://php.net/manual/en/function.preg-match.php 并循环通过匹配数组: http//php.net/manual/en/function.preg-match.php
This one should solve the short links problem (assuming you have curl installed): 这个应该解决短链接问题(假设你已经安装了curl):
follow redirects with curl in php 在php中跟随curl重定向
Use this here to check if the link is an image: 在此处使用此选项可检查链接是否为图像:
http://php.net/manual/en/function.get-headers.php (parse "Content-Type" for "image") http://php.net/manual/en/function.get-headers.php (解析“图像”的“内容类型”)

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM