如何在php脚本中运行url并获得响应?

如何在php脚本中运行url并获得响应?

问题描述:

我已经设置了一个类似这样的记录,如何在php脚本中运行url并获得响应?

------------------------------------------------- 
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的话,我必须存储是。任何人都可以帮助我吗?请告诉我如何在php或curl中获取运行url并获得响应。

对不起,如果我的问题是基本的,我试图得到一些关于谷歌的想法,但无法找到如此张贴在这里。在此先感谢

+3

不确定,但可能是你正在寻找这个http://stackoverflow.com/questions/2280394/how-can-i-check-if-a-url-exists-via-php ?? ?? –

+1

请检查此链接[链接](http://stackoverflow.com/questions/676949/best-way-to-determine-if-a-url-is-an-image-in-php) – madankundu

+1

请参阅此SO回答检查url是否存在http://stackoverflow.com/a/2280413/6374322 –

你可以使用卷曲,因为你的条件,你必须得到http标头curl_setopt($ch, CURLOPT_HEADER, true);,如果你不需要图像,你可以阻止与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未找到'=并非存在)