Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * cURL OOP Wrapper.
- * All cURL options (curl_setopt) are available
- * through instance variables. All options can
- * be found here: http://www.php.net/manual/en/function.curl-setopt.php
- *
- * <code>
- * require_once("class.curl.php");
- *
- * $obj = new cURL("http://3floor.dk");
- * $obj->ReturnTransfer = true;
- * $obj->FollowLocation = true;
- * echo $obj->exec();
- * </code>
- *
- * This class also contains some static methods,
- * designed for quick, easy HTTP request.
- *
- * <code>
- * require_once("class.curl.php");
- * // HTTP GET Request
- * echo cURL::get("http://3floor.dk");
- * // HTTP POST Request
- * $data = array(
- * "var1" => "val1",
- * "var2" => "val2"
- * );
- * echo cURL::post("http://3floor.dk", $data);
- * </code>
- *
- * @author Niklas Hansen <nikl@itu.dk>
- * @version 0.2
- */
- class cURL
- {
- /**
- * cURL
- *
- * @var object
- * @access private
- */
- private $cURL;
- /**
- * Array for cURL options
- *
- * @var array
- * @access private
- */
- private $options = array();
- /**
- * Constructor.
- * Initializes cURL object.
- *
- * @access public
- * @param string $url The URL for the request.
- * @return void
- */
- function __construct ($url = NULL) {
- return $this->cURL = curl_init($url);
- }
- /**
- * __set function
- * Sets an option for the cURL request. If the option is already set, the
- * existing option is removed, before setting the new value, in order
- * to ensure the correct order of the set options.
- *
- * Usage:
- * <code>
- * require_once("class.curl.php");
- *
- * $obj = new cURL("http://3floor.dk");
- * $obj->ReturnTransfer = true;
- * $obj->FollowLocation = true;
- * echo $obj->exec();
- * </code>
- *
- *
- * @access public
- * @return void
- */
- function __set ($name, $value) {
- if (array_key_exists(strtolower($name), $this->options)) {
- unset($this->options[strtolower($name)]);
- }
- $this->options[strtolower($name)] = $value;
- }
- /**
- * __get function
- * Gets an option for the cURL request.
- *
- * @access public
- * @return mixed Returns the value of the field requested. If field isn't
- * set, null will be returned.
- */
- function __get ($name) {
- if (array_key_exists(strtolower($name), $this->options)) {
- return $this->options[strtolower($name)];
- }
- return null;
- }
- /**
- * exec function.
- * Executes the cURL HTTP Request.
- *
- * @access public
- * @return mixed true/false on success/failure.
- * When ReturnTranfer is used, the result of the
- * request will be returned.
- */
- function exec () {
- foreach ($this->options as $key => $val) {
- if ($key !== "cURL" && $val !== null) {
- curl_setopt($this->cURL, constant(($key == "header_out" ? "CURLINFO_": "CURLOPT_").strtoupper($key)), $val);
- }
- }
- return curl_exec($this->cURL);
- }
- /**
- * errno function.
- * Returns error-number from the last request.
- *
- * @access public
- * @return int
- */
- function errno () {
- return curl_errno($this->cURL);
- }
- /**
- * error function.
- * Returns error message from the last request.
- *
- * @access public
- * @return string
- */
- function error () {
- return curl_error($this->cURL);
- }
- /**
- * getInfo function.
- * Returns informations about the last request.
- *
- * @access public
- * @param const $opt
- * @return mixed
- * @see http://php.net/manual/en/function.curl-getinfo.php
- */
- function getInfo ($opt = null) {
- if ($opt === null) {
- return curl_getinfo($this->cURL);
- } else {
- return curl_getinfo($this->cURL, $opt);
- }
- }
- /**
- * cURL version.
- * Returns information about the cURL version.
- *
- * @access public
- * @static
- * @return array
- */
- static function version () {
- return curl_version();
- }
- /**
- * Destructor.
- * Closes the cURL connection when the object
- * is destructed.
- *
- * @access public
- * @return void
- */
- function __destruct () {
- curl_close($this->cURL);
- }
- /**************************/
- /***** Static helpers *****/
- /**************************/
- /**
- * HTTP GET Request.
- * Makes a request to the specified URL,
- * and returns the content. If URL
- * does redirects, the script will follow
- * the redirects.
- *
- * @access public
- * @static
- * @param string $url The URL for the request.
- * @return mixed The content on success - false on failure.
- */
- static function get ($url) {
- $class = __CLASS__;
- $instance = new $class($url);
- $instance->HTTPGet = true;
- $instance->ReturnTransfer = true;
- $instance->FollowLocation = true;
- return $instance->exec();
- }
- /**
- * HTTP POST Request.
- * Makes a POST request to the specified URL,
- * and returns the content. The POST-data is
- * given as an array.
- *
- * @access public
- * @static
- * @param string $url The URL for the request.
- * @param array $data The data-array getting posted to $url.
- * @return mixed The content on success - false on failure.
- */
- static function post ($url, $data) {
- $class = __CLASS__;
- $instance = new $class($url);
- $instance->ReturnTransfer = true;
- $instance->FollowLocation = true;
- $instance->Post = 1;
- $instance->PostFields = http_build_query($data);
- return $instance->exec();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement