0

我在我的数据库中保存一些 flickr 照片的照片文件 url 有一段时间了。最近,当文件删除或更改时,我得到了默认图像s permisions are changed. After a while i came to the conclusion that the photo still exists and the permisions didn,但照片文件的 url 已更改。

例如:

照片网址:http ://www.flickr.com/photos/premnath/8127604491/

我之前保存的照片文件网址:http: //farm9.staticflickr.com/8336/8127604491_0eeb3b472d_z.jpg

有没有一种快速的方法来检查某个照片文件的 url 是否仍然可用。我想实现一个脚本来更新这些 url,如果它们在被访问的时间发生了变化。

我正在使用 phpFlickr。

4

2 回答 2

1

当我尝试从 CURL 访问图像http://farm9.staticflickr.com/8336/8127604491_0eeb3b472d_z.jpg时,我收到了移动的 HTTP 状态 302,它指向https://s.yimg.com/ pw/images/photo_unavailable_z.gif(这是标准图像不可用图像)。

您需要找到一种方法来捕获 HTTP 状态,然后对其采取行动。302 表示它已移动。200 表示图像仍然存在。

这是 PHP 中的示例代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://farm9.staticflickr.com/8336/8127604491_0eeb3b472d_z.jpg");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_exec($ch);

$info = curl_getinfo($ch);

if ($info['http_code'] == 302) {
  echo "Image has moved";
}

curl_close($ch);
于 2014-04-07T22:36:36.090 回答
0

Thanks msound for the inspiration. I didn`t think of checking the headers. So i came up with a shorter, easier to understand version of the above.

$headerInfo = get_headers( $value['photo_file_url'], 1 );
if( $headerInfo[0] != "HTTP/1.1 200 OK" ){
   // Do something
}

The get_headers function returns something like this:

Array
(
    [0] => HTTP/1.1 200 OK
    [Date] => Tue, 08 Apr 2014 14:40:33 GMT
    [Content-Type] => image/jpeg
    [Content-Length] => 326978
    [Connection] => close
    [P3P] => policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"
    [Cache-Control] => max-age=315360000,public
    [Expires] => Sun, 10 Mar 2024 12:42:04 UTC
    [Last-Modified] => Wed, 25 Jul 2012 20:40:58 GMT
    [Accept-Ranges] => bytes
    [X-Cache] => Array
        (
            [0] => HIT from photocache814.flickr.bf1.yahoo.com
            [1] => HIT from cache414.flickr.ch1.yahoo.com
        )

    [X-Cache-Lookup] => Array
        (
            [0] => HIT from photocache814.flickr.bf1.yahoo.com:85
            [1] => HIT from cache414.flickr.ch1.yahoo.com:3128
        )

    [Age] => 1561078
    [Via] => 1.1 photocache814.flickr.bf1.yahoo.com:85 (squid/2.7.STABLE9), 1.1 cache414.flickr.ch1.yahoo.com:3128 (squid/2.7.STABLE9)
)
于 2014-04-08T14:45:35.150 回答