2

Hi I have to check the URL is found or not.If the URL is available I need to load the URL in iframe.How can we do this?

My sample Code

<?php
    $url = 'http://example.com/t/urlname';
    $handle = curl_init($url);
curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);

/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);

/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
    /* Handle 404 here. */
    echo "Url Not found";

} else { ?>

                    <div id="iframe-comments">
            <iframe  src="<?php echo $url; ?>" scrolling="no" style="width: 800px; height: 800px;"> 
                </iframe>

</div>
<?php 
}

curl_close($handle);
    ?>

In this case,If URL is available it's not loading/showing the iframe Div. Please suggest the better option for loading iframe

PHP Worm...
  • 4,109
  • 1
  • 25
  • 48
Sarath
  • 1,459
  • 3
  • 22
  • 38
  • What about http://stackoverflow.com/questions/16499117/how-to-detect-an-error-404-in-an-iframe ? – Yann Bertrand Jun 09 '15 at 14:25
  • you need to check if curl succeeded at all. e.g. `if ($response === false) { die(curl_error($handle)) }`. an http code may not be available at all if the request never made it out the front door. – Marc B Jun 09 '15 at 14:25
  • http://stackoverflow.com/questions/2280394/how-can-i-check-if-a-url-exists-via-php – Ryan_W4588 Jun 09 '15 at 14:32

1 Answers1

0

Try this code:

You can do the same thing without using cURL

$url = 'http://thedemon.in';
$file_headers = @get_headers($url);

if($file_headers[0] == 'HTTP/1.1 200 OK') {

    ?>
   <div id="iframe-comments">
        <iframe  src="<?php echo $url; ?>" scrolling="no" style="width: 300px; height: 300px;"> </iframe>
    </div>
    <?php
}
else {
     echo "Url Not found";
}
PHP Worm...
  • 4,109
  • 1
  • 25
  • 48