<?php class EmailManager { var $_server; var $_account; const client_login_url = 'https://www.google.com/accounts/ClientLogin'; function __construct($params = array()) { if(!isset($params["server"])) { throw new Exception("Server name required."); } if(!isset($params["account"])) { throw new Exception("Account name required."); } $this->_account = $params["account"]; $this->_server = $params["server"]; } protected function authenticateUser($email, $password) { $postVariables = array( 'accountType' => 'GOOGLE', 'Email' => $email, 'Passwd' => $password, 'service' => 'analytics' ); $response = $this->httpRequest(EmailManager::client_login_url, null, $postVariables); } protected function httpRequest($url, $get_variables=null, $post_variables=null, $headers=null) { $interface = null; if(function_exists('curl_exec')) { $interface = 'curl'; } else { $interface = 'fopen'; } if($interface == 'curl') { return $this->curlRequest($url, $get_variables, $post_variables, $headers); } else if($interface == 'fopen') { return $this->fOpenRequest($url, $get_variables, $post_variables, $headers); } } private function curlRequest($url, $get_variables=null, $post_variables=null, $headers=null) { $ch = curl_init(); if(is_array($get_variables)) { $get_variables = '?' . str_replace('&','&',urldecode(http_build_query($get_variables))); } else { $get_variables = null; } curl_setopt($ch, CURLOPT_URL, $url . $get_variables); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //CURL doesn't like google's cert if(is_array($post_variables)) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_variables); } if(is_array($headers)) { curl_setopt($ch, CURLOPT_HTTPHEADER,$headers); } $response = curl_exec($ch); $code = curl_getinfo($ch,CURLINFO_HTTP_CODE); curl_close($ch); return array('body'=>$response,'code'=>$code); } private function fOpenRequest($url, $get_variables=null, $post_variables=null, $headers=null) { $http_options = array('method'=>'GET','timeout'=>3); if(is_array($headers)) { $headers = implode("\r\n",$headers) . "\r\n"; } else { $headers = ''; } if(is_array($get_variables)) { $get_variables = '?' . str_replace('&','&',urldecode(http_build_query($get_variables))); } else { $get_variables = null; } if(is_array($post_variables)) { $post_variables = str_replace('&','&',urldecode(http_build_query($post_variables))); $http_options['method'] = 'POST'; $headers = "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($post_variables) . "\r\n" . $headers; $http_options['header'] = $headers; $http_options['content'] = $post_variables; } else { $post_variables = ''; $http_options['header'] = $headers; } $context = stream_context_create(array('http'=>$http_options)); $response = @file_get_contents($url . $get_variables, null, $context); return array('body'=>$response!==false?$response:'Request failed, fopen provides no further information','code'=>$response!==false?'200':'400'); } } ?>
Tuesday, February 26, 2013
simple class for calling a url using either curl or fopen
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment