Here is a class that will submit post variables or cookie variables using the CURL library.
The CookiePost Class
/**
* CookiePost
* Uses CURL to submit a post and/or cookie.
*
* @author Kyle Robinson Young <kyle at kyletyoung.com>
*/
class CookiePost
{
var $useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
var $referer = 'http://www.google.com';
var $timeout = 30;
var $maxredirs = 4;
var $status;
var $post, $cookie = array();
/**
* SUBMIT
*
* @param str $url
* @return bool | str
*/
function submit($url=null)
{
if (empty($url)) return false;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($curl, CURLOPT_MAXREDIRS, $this->maxredirs);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// IF COOKIES
if ($this->getVars('cookie', '; '))
{
curl_setopt($curl, CURLOPT_COOKIE, $this->getVars('cookie'));
} // cookie
// IF POST
if ($this->getVars('post'))
{
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->getVars('post'));
} // post
curl_setopt($curl, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($curl, CURLOPT_REFERER, $this->referer);
$html = curl_exec($curl);
$this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return $html;
} // submit
/**
* GET VARS
* Returns class vars from array to str
*
* @param str $name
* @return false | str
*/
function getVars($name=null, $ender='&')
{
if (empty($this->$name)) return false;
if (!is_array($this->$name)) return $this->$name;
$vars = '';
foreach ($this->$name as $key => $val)
{
if (empty($key)) continue;
$vars .= $key.'='.$val.$ender;
} // foreach
$this->$name = substr($vars, 0, -1);
return $this->$name;
} // getVars
} // CookiePost
Using The Code
$cookiePost = new CookiePost();
$cookiePost->cookie = array(
'user' => 'kyle',
'passwd' => '1234',
'cookie' => '1'
);
$cookiePost->post = array(
'postVar1' => 'testing'
);
echo $cookiePost->submit('http://www.example.com/test.php');
First create a CookiePost object and then specify any cookies and/or post vars we want to submit using an associative array. Finally just give the submit method a URL to submit to. It will return the HTML and set the status variable to the returned HTTP status code. Enjoy.
VN:F [1.7.4_987]
Rating: 5.0/5 (2 votes cast)