drm30963 2016-09-29 07:10
浏览 41

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

I have set of records like this,

-------------------------------------------------
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    |
-------------------------------------------------

now i need to run this url in while loop and if the images loads then i have to store respose as NO & if url resturn 404 then i have to store YES. Could anyone help me on it ? Please tell me how to get run url in php or curl and get response.

Sorry if my question was basic, i tried to get some idea on google but couldn't find so posting here. Thanks in advance

  • 写回答

1条回答 默认 最新

  • doufei5315 2016-09-29 15:30
    关注

    you can use curl and because of your condition you must get http header with curl_setopt($ch, CURLOPT_HEADER, true); and if you don't need image you can prevent to get body with curl_setopt($ch, CURLOPT_NOBODY, true);. after set this option you get response like :

    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 
    

    if status code is 200 like above its exist but if status code 404 its not exist.

    the final code something like this:

    $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;
    

    you must read first character of a $server_output ('HTTP/1.1 200 OK' = exist) or ('HTTP/1.1 404 Not Found' = not exist)

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line