title: How to import contacts from Gmail using PHP
body: <p>I want to access user contacts using PHP.</p>
  
  <p>I am having two files named index.php &amp; validate.php in /var/www/oauth directory.
  After redirecting to validate.php,it is now showing any content.
  it is printing echo "before curl init" but not printing echo "after curl init".
  Please someone can help me out.</p>
  
  <p>Google account setting:-</p>
  
  <pre><code>Client ID:  *********
  Email address:  *****
  Client secret:  ******
  Redirect URIs:  http://localhost/oauth/validate.php
  JavaScript origins: http://localhost
  </code></pre>
  
  <p>Here is my PHP code.</p>
  
  <p>/var/www/oauth/index.php</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;Login with gmail&lt;/title&gt;
  &lt;style type="text/css"&gt;
  body{background:url(random_grey_variations.png) repeat; }
  #wrapper{width:960px; margin:0 auto; margin-top:100px;}
  .button{display:block; height:40px; padding:0 20px; width:200px; background:#CCC; text-decoration:none; font:bold 18px Verdana, Geneva, sans-serif; color:#333; line-height:40px; text-align:center; margin-bottom:50px;}
  &lt;/style&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;div id="wrapper"&gt;
          &lt;!-- &lt;a href="http://webdevelopergeeks.com/tutorial/import-gmail-or-google-contacts-using-php-and-oauth-2-0/" class="button"&gt;Back To Article&lt;/a&gt; --&gt;
          &lt;a href="https://accounts.google.com/o/oauth2/auth?client_id=1015115465486.apps.googleusercontent.com&amp;redirect_uri=http://localhost/oauth/validate.php&amp;scope=https://www.google.com/m8/feeds/&amp;response_type=code" class="button"&gt;Login Using Google&lt;/a&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>/var/www/oauth/validate.php</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;&lt;meta name="robots" content="noindex" /&gt;&lt;/head&gt;
      &lt;body style="font-family: tahoma; font-size: 12px;"&gt;
          &lt;a href="http://webdevelopergeeks.com/tutorial/import-gmail-or-google-contacts-using-php-and-oauth-2-0/"&gt;Visit Article&lt;/a&gt;&lt;hr&gt;
  &lt;?php
  //setting parameters
  $authcode= $_GET["code"];
  $clientid='1015115465486.apps.googleusercontent.com'; // client id
  $clientsecret='----'; //Secret id
  $redirecturi='http://localhost/oauth/validate.php'; // redirect uri [path to your validate.php]
  // echo $authcode;
  $fields=array(
      'code'=&gt;  urlencode($authcode),
      'client_id'=&gt;  urlencode($clientid),
      'client_secret'=&gt;  urlencode($clientsecret),
      'redirect_uri'=&gt;  urlencode($redirecturi),
      'grant_type'=&gt;  urlencode('authorization_code')
  );
  //url-ify the data for the POST
  $fields_string='';
  foreach($fields as $key=&gt;$value) { $fields_string .= $key.'='.$value.'&amp;'; }
  $fields_string=rtrim($fields_string,'&amp;');
  //open connection
  **echo "before curl init";**
  $ch = curl_init();
  //set the url, number of POST vars, POST data
  **echo "After curl init";**
  
  curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
  curl_setopt($ch,CURLOPT_POST,5);
  curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
  // Set so curl_exec returns the result instead of outputting it.
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  //to trust any ssl certificates
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  //execute post
  $result = curl_exec($ch);
  //close connection
  curl_close($ch);
  //extracting access_token from response string
  $response=  json_decode($result);
  $accesstoken= $response-&gt;access_token;
  //passing accesstoken to obtain contact details
  echo $accesstoken;
  $xmlresponse= file_get_contents('https://www.google.com/m8/feeds/contacts/default/full?oauth_token='.$accesstoken);
  //reading xml using SimpleXML
  $xml= new SimpleXMLElement($xmlresponse);
  $xml-&gt;registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
  $result = $xml-&gt;xpath('//gd:email');
  
  foreach ($result as $title) {
    echo $title-&gt;attributes()-&gt;address . "&lt;br&gt;&lt;br&gt;";
  }
  ?&gt;
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>