2013-01-24 3 views
1

CodeIgniter 프레임 워크와 함께 Google Contact API에 액세스하는 데 대한 게시물/자습서가 있습니까?
나는 많은 것을 봤어. 하나의 게시물을 발견 했어. Zend Codeigniter가있는 라이브러리.
Contact API의 일부 PHP 구현을 검토했습니다. 그러나 그들 모두는 Curl을 사용하여 API와 대화하고 있습니다.
Codeigniter 모델 클래스와 동일한 방법을 사용할 수 있습니까? 또는 자바 스크립트를 사용하여 Gmail 연락처에 액세스하는 것을 고려해야합니까?
PHP로 Google 연락처 API에 액세스 Codeigniter

도와주세요.

답변

3

나는에 CodeIgniter를 위해 http://getsparks.org/packages/oauth2/versions/HEAD/show

설정 범위의 OAuth 라이브러리를 사용하는 스파크> oauth-> libries-> provider-> 같은 파일에

public function __construct(array $options = array()) { 
    // Now make sure we have the default scope to get user data 
    empty($options['scope']) and $options['scope'] = array(
     'https://www.googleapis.com/auth/userinfo.profile', 
     'https://www.googleapis.com/auth/userinfo.email', 
     'https://www.google.com/m8/feeds', 
    ); 

추가 다음 코드처럼 구글

public function curl_file_get_contents($email, OAuth2_Token_Access $token) { 
    $url = "https://www.google.com/m8/feeds/contacts/$email/full?max-results=" . 25 . "&oauth_token=" . $token->access_token; 
    $curl = curl_init(); 
    $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'; 
    curl_setopt($curl, CURLOPT_URL, $url); //The URL to fetch. This can also be set when initializing a session with curl_init(). 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. 
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); //The number of seconds to wait while trying to connect. 

    curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //The contents of the "User-Agent: " header to be used in a HTTP request. 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); //To follow any "Location: " header that the server sends as part of the HTTP header. 
    curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect. 
    curl_setopt($curl, CURLOPT_TIMEOUT, 10); //The maximum number of seconds to allow cURL functions to execute. 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); //To stop cURL from verifying the peer's certificate. 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 

    $contents = curl_exec($curl); 
    curl_close($curl); 

    return $contents; 
} 

이 코드를 컨트롤러의 google 컨텐트에 사용했습니다.

public function google() { 
     $this->load->helper('url_helper'); 
     $this->load->spark('oauth2'); 
     $provider = $this->oauth2->provider('google', array(
      'id' => GOOGLE_APP_ID, 
      'secret' => GOOGLE_APP_SECRET, 
       )); 

     if (!$this->input->get('code')) { 
      // By sending no options it'll come back here 
      $provider->authorize(); 
     } else { 
      // Howzit? 
      try { 
       $token = $provider->access($_GET['code']); 

       $user = $provider->get_user_info($token); 
       $email = $user['email']; 

       $xmlresponse = $provider->curl_file_get_contents($email, $token); 
       if ((strlen(stristr($xmlresponse, 'Authorization required')) > 0) && (strlen(stristr($xmlresponse, 'Error ')) > 0)) { 
        echo "<h2>OOPS !! Something went wrong. Please try reloading the page.</h2>"; 
        exit(); 
       } 
       $xml = new SimpleXMLElement($xmlresponse); 
       $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005'); 
       $result = $xml->xpath('//gd:email'); 
       $this->data['email_address'] = $result; 
print_r($result);exit; 
      } catch (OAuth2_Exception $e) { 
       show_error('That didnt work: ' . $e); 
      } 
     } 

    } 
관련 문제