欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php获取当前网址与页面内容的代码参考

程序员文章站 2022-05-02 11:53:55
...
  1. /**

  2. * 得到当前网址
  3. * eidt by bbs.it-home.org
  4. */
  5. function get_url() {
  6. $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
  7. $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
  8. $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
  9. $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
  10. return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
  11. }
  12. //输出当前网址

  13. $url = get_url();
  14. echo $url;
  15. ?>
复制代码

2,获取页面内容的代码

  1. /**

  2. * 得到页面内容
  3. * edit by bbs.it-home.org
  4. */
  5. function get_contents($url){
  6. if(function_exists('file_get_contents')){
  7. $file_contents = file_get_contents($url);
  8. }else{
  9. $ch = curl_init();
  10. $timeout = 5;
  11. curl_setopt ($ch, CURLOPT_URL, $url);
  12. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  13. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  14. $file_contents = curl_exec($ch);
  15. curl_close($ch);
  16. }
  17. //输出内容

  18. return $file_contents;
  19. ?>
复制代码