首页 >后端开发 >php教程 >php获取当前网址与页面内容的代码参考

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

WBOY
WBOY原创
2016-07-25 08:57:29975浏览
  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. ?>
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn