2011-03-13 132 views

回答

1

正如@Gordon在评论中指出的,twitpic似乎有一个API - 您应该使用这种类型的东西。

参见:http://dev.twitpic.com/



现在,这里的老答案 - 有趣,但不是一个真正的好主意,考虑到有一个API:

您拥有的网址不是图片本身的网址:它是显示图片的HTML网页网址,网址为<img>标记。

所以,你需要在两个步骤行事:


而这里的,做的是代码的一个例子:

$twitpic_url = 'http://twitpic.com/49275c'; 

$dom = new DOMDocument(); 
if (@$dom->loadHTMLFile($twitpic_url)) { 
    // HTML loaded successfully 
    // => You need to find the right <img> tag 
    // Looking at the HTML, you'll see it has id="photo-display" 
    $img_tag = $dom->getElementById('photo-display'); 

    $src = $img_tag->getAttribute('src'); 

    // Just to be sure, let's display the image's URL 
    var_dump($src); 

    // Now, you have to download the image which URL is $src 
    $img_content = file_get_contents($src); 

    // ANd do whatever you want with that binary image content 
    // like save if to a file : 
    file_put_contents('/tmp/my-image.jpg', $img_content); 
} 

注:你要在这里和那里添加一些检查 - 象检查,如果照片显示元素存在,例如。

+0

是的,但TwitPic有一个API,可以用更少的代码和更可靠的方法来实现。 – Gordon 2011-03-13 15:10:12

+0

呃;应该检查是否有一个API ...使用它确实是一个更好的主意^^ *(我将编辑我的答案指出)* – 2011-03-13 15:11:00

+0

那里[已有答案](http:// stackoverflow 。(5290185/how-to-download-photo-from-twitpic/5290189#5290189)虽然;) – Gordon 2011-03-13 15:11:39