DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Fetch Contents Via HTTP(S)
This is a simple class for fetch text via Http or Https protocal, using cURL.
<?php
class Http_Fetch{
var $secure;
var $useragent;
var $url;
var $header;
var $curl;
var $curl_info = array();
var $response;
function __construct($url = null){
if ($url){
$this->set_url($url);
}
}
function set_url($url){
$url_array = parse_url($url);
if ($url_array['scheme'] == 'https'){
$this->secure = true;
}else{
$this->secure = false;
}
$this->url = $url;
$host = $url_array['host'];
}
function set_useragent($useragent){
$this->useragent = $useragent;
}
function set_header($header){
if (is_array($header)){
$this->header = $header;
}else{
$this->header = explode("\n", $header);
}
}
function connect(){
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_URL, $this->url);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_HEADER, 0);
if ($this->header){
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->header);
}
if ($this->secure){
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 2);
}
$this->response = curl_exec($this->curl);
$this->curl_info = curl_getinfo($this->curl);
}
function get_response_body(){
return $this->response;
}
function get_response_code(){
return $this->curl_info['http_code'];
}
function close(){
curl_close($this->curl);
}
}
?>





