使用PHP检查URL是否可用.如果URL可用,则加载iframe

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

本文关键字:URL 可用 如果 加载 iframe PHP 检查 是否 使用      更新时间:2023-09-26

嗨,我必须检查URL是否被找到。如果URL可用,我需要在iframe中加载URL。我们如何做到这一点?

我的示例代码

<?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);
    ?>

在这种情况下,如果URL是可用的,它不加载/显示iframe Div。请建议更好的选项加载iframe

试试下面的代码:

不使用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";
}