Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从twitpic url下载照片_Php_Ruby_Parsing - Fatal编程技术网

Php 从twitpic url下载照片

Php 从twitpic url下载照片,php,ruby,parsing,Php,Ruby,Parsing,有没有办法从Twitpic URL下载图像?假设我想拍下一张照片。正如@Gordon在评论中指出的,twitpic似乎有一个API——你应该使用它来做这类事情 请参阅: 现在,这里有一个古老的答案——有趣,但考虑到有一个API,这不是一个好主意: 您拥有的URL不是图像本身的URL:它是HTML页面的URL,在getAttribute('src')中显示图像; //为了确定,让我们显示图像的URL var_dump($src); //现在,您必须下载URL为$src的图像 $img\u co

有没有办法从Twitpic URL下载图像?假设我想拍下一张照片。

正如@Gordon在评论中指出的,twitpic似乎有一个API——你应该使用它来做这类事情

请参阅:



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

您拥有的URL不是图像本身的URL:它是HTML页面的URL,在
getAttribute('src')中显示图像;
//为了确定,让我们显示图像的URL
var_dump($src);
//现在,您必须下载URL为$src的图像
$img\u content=file\u get\u contents($src);
//然后你可以用二进制图像内容做任何你想做的事情
//与保存到文件类似:
文件内容('/tmp/my image.jpg',$img\u content);
}

注意:您必须在这里和那里添加一些检查,例如检查照片显示元素是否存在。

ID为
49275c
的图像的相应链接由

对于全尺寸图像

对于不同的尺寸,将“满”替换为“拇指”或“迷你”

$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);
}
您应该真正了解一下API和tinker:

你可以直接通过Twitter API,使用。

是的,但是TwitPic有一个API,它可以用更少的代码和更可靠的方式完成;应该检查是否有API。。。使用它确实是一个更好的主意^^(我将编辑我的答案,指出这一点),尽管如此;)地狱:-(这是在我编辑我的答案以提供一个示例时发布的^^^好吧,另一个答案已经被接受,这是正确的做法:-)
http://twitpic.com/show/[size]/[image-id]