Check URL is available Using PHP. if URL is available load the iframe

735 views Asked by At

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

1

There are 1 answers

0
PHP Worm... On

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";
}