/** * Quickly pulls data from a URI. This only works with GET requests but * can handle HTTP Basic Auth * * @param string uri the url to pull from * @param string username the username for the service [Optional] * @param string password the password for the user [Optional] * @return string * @throws Kohana_User_Exception * @author Sam Clark * @access public * @static **/ public static function pull($uri, $username = FALSE, $password = FALSE) { if (!valid::url($uri)) { throw new Kohana_User_Exception('Curl::pull()', 'The URL : ' . $uri . ' is not a valid resource'); } // Initiate a curl session based on the URL supplied $curl = Curl::factory(array(CURLOPT_POST => FALSE), $uri); // If a username/password is supplied if ($username and $password) { // Add the HTTP Basic Auth headers $curl->setopt_array(array(CURLOPT_USERPWD => $username . ':' . $password)); } // Launch the request and return the result return $curl->exec()->result(); }
/** * Post * Execute an HTTP POST request, posting the past parameters * @param string $url url to request * @param array $data past data to post to $url * @param array $headers additional headers to send in the request * @param bool $headers_only flag to return only the headers * @param array $curl_options additional curl options to instantiate curl with * @return string result */ public static function post($url, array $data = array(), array $headers = array(), $headers_only = FALSE, array $curl_options = array()) { $ch = Curl::factory($curl_options); $ch->set_opt(CURLOPT_URL, $url)->set_opt(CURLOPT_NOBODY, $headers_only)->set_opt(CURLOPT_RETURNTRANSFER, TRUE)->set_opt(CURLOPT_POST, TRUE)->set_opt(CURLOPT_POSTFIELDS, $data); //Set any additional headers if (!empty($headers)) { $ch->set_opt(CURLOPT_HTTPHEADER, $headers); } return $ch->execute(); }
/** * Reloads the cURL library after execution * * @return void * @author Sam Clark * @access public */ public function reload_curl() { // If there is no cURL library or cURL has executed if ($this->curl === NULL or $this->curl->executed) { $this->curl = Curl::factory(array(CURLOPT_POST => FALSE)); } return; }
/** * Quickly gets data from a URI. This only works with GET requests but * can handle HTTP Basic Auth * * @param string uri the url to pull from * @param string username the username for the service [Optional] * @param string password the password for the user [Optional] * @return string * @return void * @throws Kohana_User_Exception * @access public * @static **/ public static function get($url, $username = FALSE, $password = FALSE) { if (!valid::url($url)) { throw new Kohana_User_Exception(__CLASS__ . '.' . __METHOD__ . '()', 'The URL : ' . $url . ' is not a valid resource'); } // Initiate a curl session based on the URL supplied $curl = Curl::factory(array(CURLOPT_POST => FALSE), $url); // If a username/password is supplied if ($username and $password) { // Add the HTTP Basic Auth headers $curl->setopt_array(array(CURLOPT_USERPWD => $username . ':' . $password)); } // Run the curl request $curl->exec(); // If there was an error, return null if ($curl->error()) { return; } else { return $curl->result(); } }