使用PHP从twitter页面中刮取图片网址
问题描述:
我试图从twitter中抓取图片url 'https://pbs.twimg.com/media/BGZHCHwCEAACJ19.jpg:large'使用php。我发现下面的php代码和file_get_contents正在工作,但我不认为正则表达式是匹配的网址。你能帮助调试这段代码吗?提前致谢。使用PHP从twitter页面中刮取图片网址
以下是Twitter的一个片段包含影像:
<div class="media-gallery-image-wrapper">
<img class="large media-slideshow-image" alt="" src="https://pbs.twimg.com/media/BGZHCHwCEAACJ19.jpg:large" height="480" width="358">
</div>
这里是PHP代码:
<?php
$url = 'http://t.co/s54fJgrzrG';
$twitter_page = file_get_contents($url);
preg_match('/(http:\/\/p.twimg.com\/[^:]+):/i', $twitter_page, $matches);
$imgURL = array_pop($matches);
echo $imgURL;
?>
答
像这样的东西应该提供一个URL。
<?php
$url = 'http://t.co/s54fJgrzrG';
$twitter_page = file_get_contents($url);
preg_match_all('!http[s]?:\/\/pbs\.twimg\.com\/[^:]+\.(jpg|png|gif)!i', $twitter_page,$matches);
echo $img_url=$matches[0][0];
?>
响应是
https://pbs.twimg.com/media/BGZHCHwCEAACJ19.jpg
答
看来,你的正则表达式缺少URI的开头部分。它缺少'pbs'部分,无法确定http或https。
preg_match('/((http|https):\/\/pbs.twimg.com\/[^:]+):/i', $twitter_page, $matches);
谢谢这个工程:) – 2013-03-27 23:25:16