我有这样的记录,
-------------------------------------------------
id_detail | id_url | id_name |
-------------------------------------------------
43454 | xyz.com/xxx/yyyy.jpg | yyyyy |
43674 | xyz.com/zzzz/ddd.jpg | rrrr |
48884 | xyz.com/aaaa/eee.jpg | mmmm |
....
....
49994 | xyz.com/cccc/fff.jpg | uuuu |
-------------------------------------------------
现在我需要在while循环中运行此url,如果图像加载,那么我必须存储respose为NO&如果url resturn 404那么我必须存储YES。有人可以帮我吗?请告诉我如何在php或curl中运行url并获得响应。
很抱歉,如果我的问题是基本的,我试图在谷歌上得到一些想法,但无法在这里发布。提前致谢
答案 0 :(得分:0)
你可以使用curl,因为你的情况你必须使用curl_setopt($ch, CURLOPT_HEADER, true);
获得http标头,如果你不需要图像,你可以阻止身体curl_setopt($ch, CURLOPT_NOBODY, true);
。设置此选项后,您会得到如下响应:
HTTP/1.1 200 OK Date: Thu, 29 Sep 2016 15:17:34 GMT Server: Apache Last-Modified: Sun, 15 Nov 2015 06:45:21 GMT Accept-Ranges: bytes Content-Length: 50822 Content-Type: image/png Age: 423 Expires: Wed, 05 Oct 2016 06:06:51 GMT
如果状态代码是200,就像它存在的那样,但如果状态代码404不存在,那么
最终代码如下:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"xyz.com/xxx/yyyy.jpg");
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
echo $server_output;
你必须阅读$ server_output的第一个字符(' HTTP / 1.1 200 OK' =存在)或(' HTTP / 1.1 404 Not Found' =不存在)