2013-03-27 70 views
1

我試圖從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; 
?> 

回答

1

像這樣的東西應該提供一個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 
+0

謝謝這個工程:) – 2013-03-27 23:25:16

1

看來,你的正則表達式缺少URI的開頭部分。它缺少'pbs'部分,無法確定http或https。

preg_match('/((http|https):\/\/pbs.twimg.com\/[^:]+):/i', $twitter_page, $matches);