Javascript 使用PHP检查URL是否可用。如果URL可用,请加载iframe
您好,我必须检查URL是否找到。如果URL可用,我需要在Javascript 使用PHP检查URL是否可用。如果URL可用,请加载iframe,javascript,php,iframe,Javascript,Php,Iframe,您好,我必须检查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
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);
?>
请尝试以下代码:
不使用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";
}
$url='1!'http://thedemon.in';
$file\u headers=@get\u headers($url);
如果($file_headers[0]='HTTP/1.1 200 OK'){
?>
那么呢?您需要检查curl是否成功。例如,if($response==false){die(curl\u error($handle))}
。如果请求从未从前门发出,http代码可能根本不可用。