0

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

5
  • 3
    Not sure but may be you are looking for this stackoverflow.com/questions/2280394/… ?? Commented Sep 29, 2016 at 7:13
  • 1
    Please check this link link Commented Sep 29, 2016 at 7:13
  • 1
    Refer this SO answer to check whether the url exists or not stackoverflow.com/a/2280413/6374322 Commented Sep 29, 2016 at 7:16
  • thanks @SanojSharma , madan , vignesh Commented Sep 29, 2016 at 7:21
  • @Munna, did you found the solution? Commented Sep 29, 2016 at 7:55

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.