Участник
Покинул форум
Сообщений всего: 1459
Дата рег-ции: Авг. 2008
Откуда: Крым
Помог: 11 раз(а)
|
Предлагаю выложить на php.su для всех
PHP:
скопировать код в буфер обмена
<?PHP /* * Copyright ALEN * The script is laid out in the free use * Revenue Sharing STRICTLY PROHIBITED * Do not take responsibility for the use of this class * 11/25/2010 * * Авторские права пренадлежат ALEN * Скрипт выложен в свободное пользование * Коммерческое распространение СТРОГО ЗАПРЕЩЕНО * Не несу ответственность за использование данного класса * 25.11.2010 */ class cms_http_parse { public $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 $tmp_dir='./tmp/parser_file/'; // папка для временных файлов public $time_curl=5; // время ожидания запроса к одной странице public $cache_dir='./tmp/cache_parser/'; // папка с файлами кэша public $file_coockies='./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 function get_parse($url){ 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->sleep_time>0){ sleep($this->sleep_time); } $this->ch_curl=$ch; return $page; } 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); } return $page; } } 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(); return $page; } } function get_followlocation($url){ $page=$this->get_parse($url); $page=$this->get_followlocation(trim($link[1 ][0 ])); } return $page; } 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; }} 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 { $charset=$this->charset_ifno; } return $charset; } function get($url){ $this->url=$url; $page=$this->if_get_parse($url); return $page; } function close_curle(){ } }
Использование:
PHP:
скопировать код в буфер обмена
$parser=new cms_http_parse; #$parser->on_cached=1; #$parser->post_array='user=name&pass=password'; echo $parser->get('http://php.su/');
P.S. Все данные по умолчанию сохраняются и выводятся в UTF-8 , делал для себя вообще, но если кто-то заметит недоработки и будут предложения по улучшению, можете писать. Так же класс обходит необходимость включения followlocation - что иногда бывает проблемой.
(Отредактировано автором: 25 Января, 2011 - 11:45:20)
|