Hotmail contacts with API - php

Is there any example code of importing contacts from Hotmail/Live mail using their API in PHP?
Thanks.

I suggest you taking a look at Windows Live Contacts for PHP and their simplest example:
<?php
// COPIED FROM http://livecontactsphp.codeplex.com/SourceControl/changeset/view/6336#150263
// Example of how to use the library -- contents put in $ret_array
include "contacts_fn.php";
$ret_array = get_people_array();
//to see a array dump...
var_dump($ret_array);
?>
I haven't used that myself, but it looks like thing you are after.

Yes Here is the complete code.
A)- Create a Live.com application in “Microsoft account Developer Center” for obtaining your Client ID and Client Secret.
1)- create a login.php page and paste the following code in it.
<?php
session_start();
$client_id = 'YOUR CLIENT ID';
$client_secret = 'YOUR CLIENT SECRETE';
$redirect_uri = 'http://example.com/callback.php';
$urls_ = 'https://login.live.com /oauth20_authorize.srf?client_id='.$client_id.'&scope=wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails&response_type=code&redirect_uri='.$redirect_uri;
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Import Hotmail Contacts</title>
</head>
<body>
<?php echo 'Contact From MSN/Hotmail/outlook';?>
</body>
</html>
2) - Now create index.php or callback.php //it will be used as redirected page.
<?php
//function for parsing the curl request
function curl_file_get_contents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$client_id = 'YOUR CLIENT ID';
$client_secret = 'YOUR CLIENT SECRET';
$redirect_uri = 'http://example.com/callback.php';
$auth_code = $_GET["code"];
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
'redirect_uri'=> urlencode($redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://login.live.com/oauth20_token.srf');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
$accesstoken = $response->access_token;
$url = 'https://apis.live.net/v5.0/me/contacts?access_token='.$accesstoken.'&limit=2';
$xmlresponse = curl_file_get_contents($url);
$xml = json_decode($xmlresponse, true);
$msn_email = "";
foreach($xml['data'] as $emails)
{
// echo $emails['name'];
$email_ids = implode(",",array_unique($emails['emails']));
$msn_email .= "<div><span>".$emails['name']."</span>=> <span>". rtrim($email_ids,",")."</span></div>";
}
echo $msn_email;
?>

Related

Instagram API not returning json

The login is working fine. But I can't get the data via the access_token. How to get the data? My app is in Sandbox Mode by the way. Does it makes a difference? I am logging in using the same account as the admin of the app.
<?php
set_time_limit(0);
ini_set('default_socket_timeout', 300);
session_start();
// instagram details
define('clientID', MY-ID);
define('clientSecret', MY-SECRET);
define('redirectURI', 'http://localhost/pixela/insta_test.php');
// check login
$link = 'Login to Instagram';
if (isset($_GET["code"])) {
$code = $_GET["code"];
$url = 'http://api.instagram.com/oauth/access_token';
$access_token_settings = array (
'client_id' => clientID,
'client_secret' => clientSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => redirectURI,
'code' => $code
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $access_token_settings);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
$results = json_decode($result, true);
echo 'u-'.$results['user']['username'];
$link = 'Get photos';
}
?>
<!doctype html>
<html>
<head>
<title>Testing Instagram API</title>
</head>
<body>
<?php echo $link; ?>
</body>
</html>
When not logged in
When logged in

Can't seem to install the google-api-php-client correctly

There is something going wrong with the google api php client. Even though I followed the installation on github precisely it still wont work. Im not using the Composer way but i downloaded the release. I have tried to use different paths in the response-callback.php require_once like
'google-api-php-client/src/google/autoload.php'
'google-api-php-client/vendor_autoload.php'
But nothing worked...
Error:
Fatal error: require_once(): Failed opening required 'google-api-php-client/vendor/autoload.php' (include_path='.:/Users/mike/Documents/test:/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/php/sdk') in /Users/mike/Documents/test/response-callback.php on line 3
Warning: require_once(google-api-php-client/vendor/autoload.php): failed to open stream: No such file or directory in /Users/mike/Documents/test/response-callback.php on line 3
app.yaml:
application: csimporttest
version: 1
runtime: php55
api_version: 1
handlers:
- url: /.*
script: main.php
main.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Hi! Stackoverflow!</h2>
<?php include'response-callback.php';
echo "response-callback file: ", $test;
?>
Import google contacts
</body>
</html>
response-callback.php
<?php
require_once 'google-api-php-client/vendor/autoload.php';
$test = 'WORKS!';
$google_client_id = 'SECRET';
$google_client_secret = 'SECRET';
$google_redirect_uri = 'https://csimporttest.appspot.com';
//setup new google client
$client = new Google_Client();
$client -> setApplicationName('csimporttest');
$client -> setClientid($google_client_id);
$client -> setClientSecret($google_client_secret);
$client -> setRedirectUri($google_redirect_uri);
$client -> setAccessType('online');
$client -> setScopes('https://www.googleapis.com/auth/contacts.readonly');
$googleImportUrl = $client -> createAuthUrl();
function curl($url, $post = "") {
$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.
if ($post != "") {
curl_setopt($curl, CURLOPT_POST, 5);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
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, FALSE);
//To stop cURL from verifying the peer's certificate.
$contents = curl_exec($curl);
curl_close($curl);
return $contents;
}
if(isset($_SESSION['google_code'])) {
$auth_code = $_SESSION['google_code'];
$max_results = 200;
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($google_client_id),
'client_secret'=> urlencode($google_client_secret),
'redirect_uri'=> urlencode($google_redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value)
{
$post .= $key.'='.$value.'&';
}
$post = rtrim($post,'&');
$result = curl('https://www.googleapis.com/oauth2/v4/token',$post);
$response = json_decode($result);
$accesstoken = $response->access_token;
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
$xmlresponse = curl($url);
$contacts = json_decode($xmlresponse,true);
$return = array();
if (!empty($contacts['feed']['entry'])) {
foreach($contacts['feed']['entry'] as $contact) {
//retrieve Name and email address
$return[] = array (
'name'=> $contact['title']['$t'],
'email' => $contact['gd$email'][0]['address'],
);
}
}
$google_contacts = $return;
unset($_SESSION['google_code']);
}
?>
Just put the whole Google php client library in your project folder, then change
require_once 'google-api-php-client/vendor/autoload.php';
to:
require_once 'google-api-php-client-1-master/src/Google/autoload.php';

My website won't show any contacts?

When I click on the <a> which stands in main.php to import the contacts and I click through the accepts of google so I allow them to see my contacts. And then when I return to own page... it doesn't show any contacts at all. I think it's because I don't return any contacts, but I don't know how.
app.yaml:
application: csimporttest
version: 1
runtime: php55
api_version: 1
handlers:
- url: /.*
script: main.php
main.php:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Hi!</h2>
<?php include('response-callback.php');?>
Import
</body>
</html>
response-callback.php;
<?php
session_start();
require_once 'google-api-php-client-master/src/Google/autoload.php';
$google_client_id = 'SECRET';
$google_client_secret = 'SECRET';
$google_redirect_uri = 'https://csimporttest.appspot.com/response-callback.php';
//setup new google client
$client = new Google_Client();
$client -> setApplicationName('csimporttest');
$client -> setClientid($google_client_id);
$client -> setClientSecret($google_client_secret);
$client -> setRedirectUri($google_redirect_uri);
$client -> setAccessType('online');
$client -> setScopes('https://www.googleapis.com/auth/contacts.readonly');
$googleImportUrl = $client -> createAuthUrl();
function curl($url, $post = "") {
$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.
if ($post != "") {
curl_setopt($curl, CURLOPT_POST, 5);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
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, FALSE);
//To stop cURL from verifying the peer's certificate.
$contents = curl_exec($curl);
curl_close($curl);
return $contents;
}
//google response with contact. We set a session and redirect back
if (isset($_GET['code'])) {
$auth_code = $_GET["code"];
$_SESSION['google_code'] = $auth_code;
header('Location: ' . $google_redirect_uri);
}
if(isset($_SESSION['google_code'])) {
$auth_code = $_SESSION['google_code'];
$max_results = 200;
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($google_client_id),
'client_secret'=> urlencode($google_client_secret),
'redirect_uri'=> urlencode($google_redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value)
{
$post .= $key.'='.$value.'&';
}
$post = rtrim($post,'&');
$result = curl('https://www.googleapis.com/oauth2/v4/token',$post);
$response = json_decode($result);
$accesstoken = $response->access_token;
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
$xmlresponse = curl($url);
$contacts = json_decode($xmlresponse,true);
$return = array();
if (!empty($contacts['feed']['entry'])) {
foreach($contacts['feed']['entry'] as $contact) {
//retrieve Name and email address
$return[] = array (
'name'=> $contact['title']['$t'],
'email' => $contact['gd$email'][0]['address'],
);
}
}
$google_contacts = $return;
unset($_SESSION['google_code']);
}
if (!empty($contacts['feed']['entry'])) {
foreach($contacts['feed']['entry'] as $contact) {
//retrieve Name and email address
$return[] = array (
'name'=> $contact['title']['$t'],
'email' => $contact['gd$email'][0]['address'],
);
//retrieve user photo
if (isset($contact['link'][0]['href'])) {
$url = $contact['link'][0]['href'];
$url = $url . '&access_token=' . urlencode($accesstoken);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$image = curl_exec($curl);
curl_close($curl);
}
$return['image'] = $image;
echo '';
}
}
?>
The last thing the code does is set '$google_contacts = $return', so it's up to your app to do something useful with the $google_contacts array, such as display it. The blog where this code originated doesn't mention that the code sample displays anything, only that it is an example of how to retrieve contacts.
You can use an var_dump() to see if the array google_contacts is actually filled. Put this in the main.php:
<?php
var_dump($google_contacts);
?>
If you get an output in the var_dump you can use an html table and an easy foreach, look below:
<table border='1' class="table table-striped table-condensed">
<thead>
<tr>
<th>Naam </th>
<th>Email </th>
</tr>
</thead>
<tbody>
<?php
foreach ($google_contacts as $contacts){
echo'<tr>';
echo'<td>'. $contacts['name']."</td>";
echo'<td>'. $contacts['email'].'</td>';
echo'</tr>';
}
?>
</tbody>
</table>

How to get Hotmail user profile data from PHP?

How to get Hotmail user profile data and email ID from PHP?
Is there any API available for the same?
Any help will be appreciable.
There no such official API for hotmail/outlook/live services from microsoft.
However you can use RESTful calls using Identity API to get user details.
This might help
Getting the user's name and profile picture using REST
Getting and working with user contact info using REST
here is the code for get hotmail user profile detail
<?php
$client_id = 'Your client id';
$client_secret = 'client secret key';
$redirect_uri = 'redirect url';
$auth_code = $_GET["code"];
// get contacts
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
'redirect_uri'=> urlencode($redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://login.live.com/oauth20_token.srf');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
$accesstoken = $response->access_token;
$my_url = "https://apis.live.net/v5.0/me?access_token=".$accesstoken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$my_url);
$myres=curl_exec($ch);
curl_close($ch);
$myxml = json_decode($myres, true);
$myemailid = $myxml['emails']['account'];
$username = $myxml['name'];
?>

Google Contacts Import to Mysql in PHP via OAuth

i was creating a small app using jquery mobile with server side language as php,so i want to import all my google contacts (Names,Numbers & Email) into a mysql database using the OAuth. Currently im using the following script which can grab only email addresses but is there a way for the same to import names & numbers as well?
<html>
<head>
<meta name="robots" content="noindex" />
<title>Email address list - Import Gmail or Google contacts</title>
<style type="text/css">
a:link {color:Chocolate;text-decoration: none;}
a:hover {color:CornflowerBlue;}
.logo{width:100%;height:110px;border:2px solid black;background-color:#666666;}
</style>
</head>
<body>
<div class="logo" >
<a href="http://25labs.com/" >
<img style="padding-top: 10px;" src="http://25labs.com/wp-content/themes/TheStyle/images/logo.png"></img>
</a>
</div>
<br/>
<div><b>Visit Tutorial: </b><a style="font-size:17px;" href="http://25labs.com/import-gmail-or-google-contacts-using-google-contacts-data-api-3-0-and-oauth-2-0-in-php/" >Import Gmail or Google contacts using Google Contacts Data API 3.0 and OAuth 2.0 in PHP</a></div>
<br/>
<div style="padding-left: 50px;">
<?php
$client_id = '';
$client_secret = '';
$redirect_uri = '';
$max_results = 1000;
$auth_code = $_GET["code"];
function curl_file_get_contents($url)
{
$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;
}
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
'redirect_uri'=> urlencode($redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
$accesstoken = $response->access_token;
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&oauth_token='.$accesstoken;
$xmlresponse = curl_file_get_contents($url);
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();
}
echo "<h3>Email Addresses:</h3>";
$xml = new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$result = $xml->xpath('//gd:email');
foreach ($result as $title) {
echo $title->attributes()->address . "<br>";
}
?>
</div>
</body></html>
This code uses ZF2 client but you can get the idea:
$client = new \Zend\Http\Client('https://www.google.com/m8/feeds/contacts/default/full?max-results=100000&alt=json', array(
'adapter' => 'Zend\Http\Client\Adapter\Curl'
));
$client->setHeaders(array(
array('Authorization' => 'Bearer ' . $ACCESS_TOKEN)
));
$client->send();
if ($client->getResponse()->getStatusCode() != 200) {
return false;
} else {
$output = json_decode($client->getResponse()->getBody());
if ($output) {
$entries = $output->feed->entry;
foreach ($entries as $e) {
print_r($e); // here you can see all the available information
}
}
}

Categories