一键远程获取网站信息php实例教程,

 'Invalid URL']);
  exit;
}

$contents = '';
$title = '';
$description = '';

try {
  $contents = @file_get_contents('https://' . $url);
  
  if (!$contents) {
 $contents = @file_get_contents('http://' . $url);
  }

  if ($contents) {
 preg_match("/\(.*)<\/title\>/i", $contents, $matches);
 if (isset($matches[1])) {
$title = trim($matches[1]);
 }

 preg_match_all("/\]*name=[\'\"]description[\'\"][^\>]*content=[\'\"]([^\'\"]*)[\'\"][^\>]*\>/i", $contents, $matches);
 if (isset($matches[1][0])) {
$description = trim($matches[1][0]);
 }
  }
  
  if ($title || $description) {
 echo json_encode(['title' => $title, 'description' => $description]);
  } else {
 echo json_encode(['error' => 'No title or description found']);
  }
} catch (Exception $e) {
  echo json_encode(['error' => $e->getMessage()]);
}

这段代码是一个简单的PHP脚本。该脚本的作用是获取一个 URL 中的页面标题和描述,并以 JSON 格式输出结果。

首先,脚本会读取 GET 请求中传递的 URL,并进行判断。如果 URL 为空,则输出一个错误消息。

之后,脚本会尝试使用 file_get_contents() 函数获取 URL 所指向的页面的内容。由于该页面可能使用了 HTTPS 协议,因此会先尝试使用 HTTPS 协议获取页面内容。如果失败,则使用 HTTP 协议获取。

获取到页面内容后,脚本会使用正则表达式从页面内容中提取出标题和描述信息。其中,获取标题的正则表达式为 /(.*)<\/title>/i</code>,获取描述的正则表达式为 <code>/<meta\s+[^\>]*name=[\'\"]description[\'\"][^\>]*content=[\'\"]([^\'\"]*)[\'\"][^\>]*>/i</code>。</p><p>最后,如果成功获取到了标题或描述信息,则将其以 JSON 格式输出;否则,输出错误消息。</p><p>需要注意的是,由于该脚本使用了 <code>@</code> 符号,因此在进行文件读取操作时会忽略警告消息。这并不是一个好的编程实践,应该尽可能避免使用 <code>@</code> 符号。</p><p>js如下,不一定需要用到</p><pre><code>var urlInput = document.getElementById("url-input"); urlInput.addEventListener("blur", function () { var url = urlInput.value.trim(); // 如果值以 http:// 或 https:// 开头,或以 / 结尾,则替换为空 if ( url.startsWith("http://") || url.startsWith("https://") || url.endsWith("/") ) { url = url.replace(/^(https?:\/\/)|\/$/g, ""); urlInput.value = url; } });</code></pre><p>这段代码实现了一个监听input输入框失去焦点事件的功能。当输入框失去焦点时,会获取输入框中的值,并对其进行处理:</p><p>如果输入框中的值以 <a rel="external nofollow" rel="external nofollow" href="http://">http://</a> 或 <a rel="external nofollow" rel="external nofollow" href="https://">https://</a> 开头,或以 / 结尾,则将这些字符串替换为空。具体地,通过正则表达式 <code>/(^https?:\/\/)|\/$/g</code> ,匹配以 <a rel="external nofollow" rel="external nofollow" href="http://">http://</a> 或 <a rel="external nofollow" rel="external nofollow" href="https://">https://</a> 开头或以 / 结尾的子串,并将其替换为空字符串。然后将处理后的值再次赋回到输入框中。</p><p>例如,如果输入框中的值为 "http://example.com/",则经过处理后,输入框中的值为 "example.com"。这样做的目的可能是为了去除掉一些无用的字符串,以便后续处理。</p> <div class="xcus-htmlcache-butdiv" data-artid="1239"></div> <div class="expired-notice expired-notice-open-old" rel=nofollow > <span class="expired-symbols">!</span><span class="expired-notice-banner-msg">如链接失效请在下方留言。本站所有资源均来源于网络,版权属于原作者!仅供学习参考,本站不对您的使用负任何责任。如果有侵权之处请第一时间联系我们删除,敬请谅解!</span> </div></article></div><div class="container grid-sm post-page"> <div class="pull-left theprev"><a href="https://www.te260.com/blog/1732762884.html" title="CEF应用典型特征——ChromiumCEF框架"> 上一篇 </a></div><div class="pull-right thenext"><a href="https://www.te260.com/blog/1732762174.html" title=""强大的任务管理:WordPress工单插件""> 下一篇 </a></div></div><div id="comments" class="container grid-sm s-content comments"><div class="column col-12"><!--评论框--><h4>发表评论</h4><div id="divCommentPost" class="cpost"><form class="comment-form" id="frmSumbit" target="_self" method="post" action="https://www.te260.com/zb_system/cmd.php?act=cmt&postid=1239&key=bf400b64cc76b85227ed397e07376e88" ><input type="hidden" name="inpId" id="inpId" value="1239"><input type="hidden" name="inpRevID" id="inpRevID" value="0"><div class="erx-flex post-bot"><button name="sumbit" type="submit" tabindex="6" value="提交" onclick="return zbp.comment.post()" class="submit">评论</button><a id="cancel-reply" href="#divCommentPost" style="display:none;" rel="nofollow">取消回复✕</a></div></form><div class="comment-hr"></div></div><label id="AjaxCommentBegin"></label><!--评论输出--><div class="comment-list"></div><!--评论翻页条输出--><div class="erx-tct erx-num-font erx-pagebar commentpagebar lists-navigator"></div><label id="AjaxCommentEnd"></label></div></div><div class="container grid-sm s-content footer"> <div class="column col-12"> <p>Copyright 小绿资源网 Rights Reserved. </p> <p>本站所发布的全部内容源于互联网搬运,仅限于小范围内传播学习和文献参考,请在下载后24小时内删除,如果有侵权之处请第一时间联系我们删除。敬请谅解! </p> </div> <div class="ft"><script type="text/javascript" src="https://www.te260.com/zb_users/theme/erx_Special/skin/js/language.js"></script><a id="top" href="#totop" title="返回顶部">TOP</a></div></div><script>window.onscroll=function(){var box=document.getElementById('top');var osTop=document.documentElement.scrollTop||document.body.scrollTop;if(osTop>200){box.style.display="block"}else if(osTop<=200){box.style.display="none"}}</script><script type="text/JavaScript">$(document).ready(function(){$('a[href*=#],area[href*=#]').click(function(){if(location.pathname.replace(/^\//,'')==this.pathname.replace(/^\//,'')&&location.hostname==this.hostname){var $target=$(this.hash);$target=$target.length&&$target||$('[name='+this.hash.slice(1)+']'); if($target.length){var targetOffset=$target.offset().top-20;$('html,body').animate({scrollTop:targetOffset},800);return false}}})});</script><script src="https://www.te260.com/zb_users/plugin/xcus/layer/layer.js"></script><script src="https://www.te260.com/zb_users/plugin/xcus/src/script/adview_pic_cpc_cpm_cpa_guanggao_gg_ads_300x250.js"></script><script src="https://www.te260.com/zb_users/plugin/xcus/theme/script.js?r=20250418155038"></script><div id="ly_cache" data-id="1239" data-end="1745252055"></div></body></html><!--114.73 ms , 24 queries , 6742kb memory , 0 error--><!--ly_cache 2025-04-21 00:14:15-->