Участник
Покинул форум
Сообщений всего: 1459
Дата рег-ции: Авг. 2008
Откуда: Крым
Помог: 11 раз(а)
|
- Класс обходит запрет некоторых хостеров на followlocation
- по желанию кэширует данные
- все данные возращаются в utf-8? автоопределение кодировки источника
!Данные кэшируются максимум на сутки и еще минут в том, что старые данные из папки нужно удалять ручками. Пока еще не реализована эта часть
PHP:
скопировать код в буфер обмена
<?PHP class cms_http_parse { protected $url; public $user_agent='Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1'; public $sleep_time=0; // пауза между запросами в секундах public $HTTPHEADER=null; // для CURLOPT_HTTPHEADER public $ENCODING=null; // для CURLOPT_ENCODING public $CURLOPTHEADER=1; // для CURLOPT_ENCODING public $COOKIE=''; // для CURLOPT_ENCODING public $time_curl=5; // время ожидания запроса к одной странице public $cache_dir=false; // папка с файлами кэша './tmp/cache_parser/' public $file_coockies=false; // файл с куками './tmp/coockies_http_get.dat' public $cache_time_limit=259200; // время хранения кэша в секундах (3 суток) public $charset='utf-8'; // кодировка результата данных и хранения кэша public $post_array; public $charset_ifno='windows-1251'; // если не указана кодировка то считать, // что windows-1251 public $pref_file_b='.dat'; // Расширения файла с базой public $pref_file_s='.html'; // Расширения файла кэша страницы public $on_cached; // включение кэширование на +1 protected function get_parse($url){ if($this->COOKIE!=''){ curl_setopt ($ch,CURLOPT_COOKIEJAR ,$this->file_coockies); curl_setopt ($ch,CURLOPT_COOKIEFILE ,$this->file_coockies); } if (isset($this->post_array)){ curl_setopt ($ch,CURLOPT_POSTFIELDS ,$this->post_array); } if ($this->HTTPHEADER!=null){ curl_setopt ($ch,CURLOPT_HTTPHEADER ,$this->HTTPHEADER); } if ($this->ENCODING!=null){ } if($this->sleep_time>0){ sleep($this->sleep_time); } $this->pagetext=$page; $this->ch_curl=$ch; return $page; } protected function if_get_parse($url){ $path_file_bd=$this->cache_dir.md5($this->url).$this->pref_file_b; $path_file_site=$this->cache_dir.md5($this->url).$this->pref_file_s; if($bd['time']>time()){ if($bd['charset']!=$this->charset){ iconv($bd['charset'],$this->charset,$page); } $this->pagetext=''; return $page; } else { $this->if_get_parse($url); } } else { $page=$this->get_followlocation($url); $charset_page=$this->charset_page_parse(); if($this->charset!=''&&$charset_page!=$this->charset){ $page=iconv($charset_page,$this->charset,$page); } if($page!=''){ $this->puts_content($page); } $this->close_curle(); $this->pagetext=''; return $page; } } protected function get_followlocation($url){ $page=$this->get_parse($url); $page=$this->get_followlocation(trim($link[1 ][0 ])); } return $page; } protected function puts_content($text){ if($this->on_cached==1){ $path_file_bd=$this->cache_dir.md5($this->url).$this->pref_file_b; $path_file_site=$this->cache_dir.md5($this->url).$this->pref_file_s; $bd['time']=time()+$this->cache_time_limit; $bd['charset']=$this->charset; $bd['url']=$this->url; }} protected function charset_page_parse(){ $content_type=curl_getinfo($this->ch_curl,CURLINFO_CONTENT_TYPE ); if(preg_match("#charset=(.+)\s*#is",$content_type)){ $charset=$chars[1][0]; } else { if(preg_match("#charset=(\'?|\"?)(.+)(\'|\"|\s)#isU",$this->pagetext)){ preg_match_all("#charset=(\'?|\"?)(.+)(\'|\"|\s)#isU",$this->pagetext,$chars); $charset=$chars[2][0]; } else { $charset=$this->charset_ifno; } } return $charset; } public function get($url,$CURLOPTHEADER=1){ $this->CURLOPTHEADER=$CURLOPTHEADER; $this->url=$url; if($this->cache_dir==false){ return $this->get_parse($url); } else { $this->cache_dir=$this->cache_dir.ceil(time()/86400 ); $this->cache_dir=$this->cache_dir."/".$namedirurl['host']."/"; return $this->if_get_parse($this->url); } } protected function close_curle(){ } }
Использование:
1)
2)
PHP:
скопировать код в буфер обмена
$parser=new cms_http_parse; $parser->on_cached=1; $parser->cache_dir='tmp/cache_parser/'; // папка с файлами кэша $parser->file_coockies='tmp/coockies_http_get.dat'; // файл с куками $content=$parser->get("http://php.su/");
P.S. Сразу говорю, что класс писал для своих задач, поэтому говорить, что не доработан не нужно, для меня все ок, где минусы знаю сам, главное что полностью решает мои потребности.
|