【发布时间】: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
【问题讨论】:
-
你需要检查 curl 是否成功。例如
if ($response === false) { die(curl_error($handle)) }。如果请求从未从前门发出,则 http 代码可能根本不可用。
标签: javascript php iframe