maexus Posted March 16, 2008 Share Posted March 16, 2008 Handling errors in general has been an area that I haven't focused on as much as I'd like but at this point, it's something I need. I have a script that connects to gmail and downloads the inbox xml file. What I would like to do is stop the script and display any error from CURL. For example, timeouts, wrong auth credentials, server down, ect... Here is the script that runs: <?php #gmail2sms v2.0 by Kylee Tilley <[email protected]> header("Content-type: text/plain"); require_once("curl.class.php"); $cell_email = NULL; ?> gmail2sms by Kylee Tilley [email protected] http://kyleetilley.name/ --- LOG FILE START --- ======================================================================== Connecting to Gmail: ======================================================================== <?php flush(); $ch = new curl(); $ch->set_url("https://mail.google.com/mail/feed/atom"); $ch->set_auth("username", "password"); $ch->debug(1); $ch->set_store_results(1); $ch->execute(); if(isset($ch->error)){ trigger_error("Couldn't connect to Gmail: {$ch->error}"); }else{ echo "Connection to Gmail secured"; } flush(); ?> ======================================================================== Inbox Output: ======================================================================== <?php echo $ch->get_results(); ?> --- LOG FILE END --- and my curl.class.php <?php class curl { private $handle; private $results; private $information; public $error; private $cookie_jar; public function __construct(){ $this->handle = curl_init(); $this->cookie_jar = tempnam('/tmp','cookie'); #$this->_prebuild(); } public function post($string){ $this->set_opt(CURLOPT_POST, 1); $this->set_opt(CURLOPT_POSTFIELDS, $string); } public function execute(){ $this->results = curl_exec($this->handle) or die(curl_error($this->handle)); $this->check_error(); $this->information = curl_getinfo($this->handle); } private function check_error(){ if(curl_errno($this->handle)){ $this->error = curl_error($this->handle); } } public function get_info($option = NULL){ return (isset($option)) ? $this->information[$option] : $this->information; } public function get_results(){ return $this->results; } public function set_opt($option, $value){ return curl_setopt($this->handle, $option, $value); } public function set_url($url){ $this->set_opt(CURLOPT_URL, $url); } public function set_auth($username, $password){ $this->set_opt(CURLOPT_USERPWD, "{$username}:{$password}"); } public function debug($bool){ $this->set_opt(CURLOPT_VERBOSE, $bool); } public function set_store_results($bool){ $this->set_opt(CURLOPT_RETURNTRANSFER, $bool); } public function use_proxy($url, $port, $username = NULL, $password = NULL){ $this->set_opt(CURLOPT_PROXYTYPE, CURLPROXY_HTTP); $this->set_opt(CURLOPT_PROXY, "{$url}:{$port}"); $this->set_opt(CURLOPT_SSL_VERIFYPEER, 0); if(isset($username) && isset($password)){ $this->set_opt(CURLOPT_PROXYUSERPWD, "{$username}:{$password}"); } } function __destruct(){ curl_close($this->handle); unset($this->cookie_jar); } } ?> Now, I may be missing something but an error should cause $this->error to populate and show the error in the if/then statement in the script. I have a feeling I'm WAY off on how to handle this but for some reason I can't wrap my mind around it. Again, with all my posts, I'm not looking for a solution, just a push in the right direction. Link to comment https://forums.phpfreaks.com/topic/96461-handling-errors-when-using-curl/ Share on other sites More sharing options...
maexus Posted March 17, 2008 Author Share Posted March 17, 2008 Bump Link to comment https://forums.phpfreaks.com/topic/96461-handling-errors-when-using-curl/#findComment-493924 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.