lib.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. if ( ! defined('BASE_PATH')) exit('No direct script access allowed');
  3. /**
  4. * 公共函数
  5. * */
  6. class lib{
  7. /**
  8. * 检查环境
  9. **/
  10. public static function check(){
  11. }
  12. /**
  13. * 是否为URL
  14. **/
  15. public static function is_url($url){
  16. }
  17. //抓取
  18. public static function fetch_url($url){
  19. switch(RUN_ENV){
  20. case 'SAE': //使用SAE FetchURL服务
  21. $f = new SaeFetchurl();
  22. if(STATIC_HOST){
  23. $f->setHeader('Host',STATIC_HOST);
  24. }
  25. $content = $f->fetch($url);
  26. return array($f->HttpCode(),$content);
  27. break;
  28. case 'BAE':
  29. case 'LOCAL':
  30. default:
  31. if(function_exists('curl_init')){
  32. //BAE或普通平台下可使用curl
  33. $ch = curl_init();
  34. curl_setopt($ch, CURLOPT_URL, $url);
  35. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  36. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  37. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  38. if(!ini_get('safe_mode')){
  39. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  40. }
  41. if(STATIC_HOST){
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: '.STATIC_HOST));
  43. }
  44. $con = curl_exec($ch);
  45. $cod = curl_getinfo($ch,CURLINFO_HTTP_CODE);
  46. return array($cod,$con);
  47. }else{
  48. //否则使用file_get_contents
  49. $content = '';
  50. if(STATIC_HOST){
  51. $opt=array('http'=>array('header'=>'Host: '.STATIC_HOST));
  52. $context=stream_context_create($opt);
  53. $content = file_get_contents($url,false,$context);
  54. }else{
  55. $content = file_get_contents($url);
  56. }
  57. list($version,$status_code,$msg) = explode(' ',$http_response_header[0], 3);
  58. return array($status_code,$content);
  59. }
  60. }
  61. }
  62. }