【问题标题】:Check URL is available Using PHP. if URL is available load the iframe使用 PHP 检查 URL 是否可用。如果 URL 可用,则加载 iframe
【发布时间】:2015-06-09 14:19:28
【问题描述】:

您好,我必须检查是否找到 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

【问题讨论】:

标签: javascript php iframe


【解决方案1】:

试试这个代码:

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

【讨论】:

    猜你喜欢
    • 2013-09-13
    • 1970-01-01
    • 2014-02-24
    • 2020-08-21
    • 1970-01-01
    • 2015-07-13
    • 2020-03-31
    • 2021-12-13
    • 2023-03-08
    相关资源
    最近更新 更多