A little while ago I started to write a curl class but never finished it as I quickly came to the conclusion that in many cases (or at least my cases) it was quicker just to call the cURL functions.
Anyway, here is my initial workings, it is by no means complete and I certainly would not class it as 'a niffty little class', it also has no error checking but it is functional for general connections. It may be of help to you or others....
Code: Select all
<?php
class EzCurl
{
var $ch;
var $url;
var $fullurl;
var $post;
var $get;
var $cookie;
var $browser;
var $buffer;
function EzCurl()
{
$this->ch = FALSE;
$this->url = FALSE;
$this->fullurl = FALSE;
$this->post = array();
$this->get = array();
$this->cookie = FALSE;
$this->browser = 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';
$this->buffer = FALSE;
$this->ch = curl_init();
}
function set_url($_url)
{
$this->url = $_url;
$this->fullurl = $this->url;
}
function set_post_val($_key, $_value)
{
$this->post[$_key] = rawurlencode($_value);
}
function set_get_val($_key, $_value)
{
$this->get[$_key] = rawurlencode($_value);
}
function cookie_file($file)
{
$this->cookie = $file;
}
function set_browser($_browser)
{
$this->browser = $_browser;
}
function get_page($_display = FALSE)
{
$this->_init_curl();
if ($_display)
{
echo $this->_exec_curl();
}
else
{
return $this->_exec_curl();
}
}
function reintialize($_keepcookie = TRUE)
{
$this->ch = FALSE;
$this->url = FALSE;
$this->fullurl = FALSE;
$this->post = array();
$this->get = array();
$this->buffer = FALSE;
$this->ch = FALSE;
if (!$_keepcookie)
{
$this->cookie = FALSE;
}
}
function _setopt_postvars()
{
curl_setopt($this->ch, CURLOPT_POST, TRUE);
$postvals = '';
foreach ($this->post as $key => $value)
{
$postvals .= $key . '=' . $value .'&';
}
$postvals = substr($postvals, 0, -1);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postvals);
}
function _setopt_getvars()
{
$this->fullurl .= '?';
foreach ($this->get as $key => $value)
{
$this->fullurl .= $key . '=' . $value .'&';
}
$this->fullurl = substr($this->fullurl, 0, -1);
}
function _setopt_cookies()
{
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookie);
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookie);
}
function _init_curl()
{
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($this->ch, CURLOPT_USERAGENT, $this->browser);
if ($this->cookie)
{
$this->_setopt_cookies();
}
if (count($this->post) > 0)
{
$this->_setopt_postvars();
}
if (count($this->get) > 0)
{
$this->_setopt_getvars();
}
curl_setopt($this->ch, CURLOPT_URL, $this->fullurl);
}
function _exec_curl()
{
$this->buffer = curl_exec($this->ch);
return $this->buffer;
}
}
?>
Example usage......
Logging on to this forum....
Code: Select all
<?php
$CH = new EzCurl();
$CH->set_url('http://forums.devnetwork.net/login.php');
$CH->set_post_val('username', 'YourUsername');
$CH->set_post_val('password', 'YourPassword');
$CH->set_post_val('login', 'Login');
$page = $CH->get_page();
// if you want to output the page immediately you can use $CH->get_page(true);
echo $page;
?>
Logging in directly to your inbox....
Code: Select all
<?php
$CH = new EzCurl();
$CH->set_url('http://forums.devnetwork.net/login.php');
$CH->set_post_val('username', 'YourUsername');
$CH->set_post_val('password', 'YourPassword');
$CH->set_post_val('login', 'Login');
$CH->set_post_val('redirect', 'privmsg.php?folder=inbox');
$page = $CH->get_page();
?>
Getting some results from Google for the search term 'php'
Code: Select all
<?php
$CH = new EzCurl();
$CH->set_url('http://www.google.com/search');
$CH->set_get_val('hl', 'en');
$CH->set_get_val('ie', 'UTF-8');
$CH->set_get_val('q', 'php');
$CH->set_get_val('btnG', 'Google+Search');
$page = $CH->get_page();
?>