Skip to content

Instantly share code, notes, and snippets.

@swvitaliy
Created January 16, 2012 18:28
Show Gist options
  • Save swvitaliy/1622185 to your computer and use it in GitHub Desktop.
Save swvitaliy/1622185 to your computer and use it in GitHub Desktop.
Simple curl class
<?php
/**
* Simple curl class.
*
* @author swvitaliy ([email protected])
*/
class CurlException extends Exception {
public function __toString () { return 'Exception ' . $this->getMessage(); }
}
class curl {
public static $global_post = array();
public static $global_get = array();
public static function exec ($url, $options = array (), $data = null) {
if (!empty(self::$global_get))
$url .= ((strpos($url, '?')===FALSE) ? '?' : '&') .
http_build_query(self::$global_get);
$ch = curl_init($url);
$options += array (
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2',
CURLOPT_TIMEOUT => 10,
CURLOPT_REFERER => 'http://www.google.com',
CURLOPT_FOLLOWLOCATION => 0,
CURLOPT_HEADER => 0,
CURLOPT_NOBODY => 0,);
if (!isset($options[CURLOPT_POSTFIELDS]) && $data) {
$options[CURLOPT_POST] = 1;
$options[CURLOPT_POSTFIELDS] = $data;
}
if (isset($options[CURLOPT_POSTFIELDS]) && is_array($options[CURLOPT_POSTFIELDS]))
$options[CURLOPT_POSTFIELDS] =
http_build_query($options[CURLOPT_POSTFIELDS]);
curl_setopt_array($ch, $options);
$r = curl_exec($ch);
if ($cn = curl_errno($ch))
throw new CurlException("curl error #{$cn} with message \"" . curl_error($ch) . "\"");
return $r;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment