Skip to content

Instantly share code, notes, and snippets.

@ken-muturi
Created July 8, 2016 09:11
Show Gist options
  • Save ken-muturi/21d794cb86ab115ed9bdee597f2e35a5 to your computer and use it in GitHub Desktop.
Save ken-muturi/21d794cb86ab115ed9bdee597f2e35a5 to your computer and use it in GitHub Desktop.
Curl
<?php
class Curl
{
var $headers;
var $user_agent;
var $compression;
var $cookie_file;
var $proxy;
public function cURL($cookies = TRUE, $cookie = 'cookies.txt', $compression = 'gzip', $proxy = '')
{
$this->headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$this->headers[] = 'Connection: Keep-Alive';
$this->headers[] = 'Content-type: '
.'application/x-www-form-urlencoded;charset=UTF-8';
$this->user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; '
.'Windows NT 5.1; .NET CLR 1.0.3705;'
.'.NET CLR 1.1.4322; Media Center PC 4.0)';
$this->compression=$compression;
$this->proxy=$proxy;
$this->cookies=$cookies;
if ($this->cookies == TRUE)
$this->cookie($cookie);
}
public static function cookie($cookie_file)
{
if (file_exists($cookie_file))
{
$this->cookie_file = $cookie_file;
} else {
fopen($cookie_file, 'w')
or $this->error('The cookie file could not be opened. '
.'Make sure this directory has the correct permissions');
$this->cookie_file=$cookie_file;
fclose($this->cookie_file);
}
}
public static function get($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
public static function post($url, $data)
{
$ua = 'Mozilla/4.0 (compatible; MSIE 7.0; '
.'Windows NT 5.1; .NET CLR 1.0.3705;'
.'.NET CLR 1.1.4322; Media Center PC 4.0)';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
public static function error($error) {
error_log($error);
die;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment