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'];
?>
Related
I have this code in python that do a request to the API of suap passing the values of dados_usuario, that I tested and is working, but I wanna write the same code in php and I dont getting it, all I got was the token with the follow code, so, anyone could help me with the second part using curl in php?
python code - https://imgur.com/a/LpZ7j4S
import requests
# Obtaining the user's token
url = 'https://suap.ifrn.edu.br/api/v2/autenticacao/token/'
#username and password are your data used to access SUAP
dados_usuario = {
'username': '',
'password': ''
}
requisicao = requests.post(url, data=dados_usuario)
if requisicao.status_code == requests.codes.ok:
token_autenticacao = requisicao.json().get('token')
print ('\n--- Token de Autenticação:\n {}\n\n'.format(token_autenticacao))
# Obtaining User Data.
url = 'https://suap.ifrn.edu.br/api/v2/minhas-informacoes/meus-dados/'
headers = {
'Authorization':'JWT {}'.format(token_autenticacao)
}
requisicao = requests.get(url, headers=headers)
if requisicao.status_code == requests.codes.ok:
retorno_json = requisicao.json()
print ('--- Dados do Usuário Logado:\n{}\n\n'.format(retorno_json))
php code - https://imgur.com/a/tkxb9Gm
<?php
$url = 'https://suap.ifrn.edu.br/api/v2/autenticacao/token/';
$user_data = [
'username' => '',
'password' => ''
];
$ch = curl_init();
//Getting the user token
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $user_data);
$result = curl_exec($ch);
//Token variable
$token = json_decode($result, true);
curl_close($ch);
echo $token["token"]."\n\n";
I need get the user date with php, until now I just have the token, and I need get it with curl.
I got the resolver with this code
$token = "token hide";
$ch = curl_init('https://suap.ifrn.edu.br/api/v2/minhas-informacoes/meus-dados/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: JWT ' . $token
));
$data = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
Is there a way to retrieve contact phone number with Google contact API
I developed PHP script to retrieve google contact using V2 and it works fine but I am getting only emails and name of the contact, I also need to retrieve a phone number.
Below is my code
index.php file
session_start();
//include google api library
require_once '/usr/share/nginx/html/dev/gcontact/vendor/autoload.php';// or wherever autoload.php is located
$google_client_id = 'google_client_id';
$google_client_secret = 'google_client_secret';
$google_redirect_uri = 'http://localhost/dev/gcontact/callback.php';
$client = new Google_Client();
$client -> setApplicationName('My application name');
$client -> setClientid($google_client_id);
$client -> setClientSecret($google_client_secret);
$client -> setRedirectUri($google_redirect_uri);
$client -> setAccessType('online');
$client -> setScopes(array('https://www.googleapis.com/auth/contacts','https://www.google.com/m8/feeds','https://www.google.com/m8/feeds/user','https://www.googleapis.com/auth/userinfo.email'));
echo $googleImportUrl = $client -> createAuthUrl();
Callback.php
require_once '/usr/share/nginx/html/dev/gcontact/vendor/autoload.php';// or wherever autoload.php is located
if (isset($_GET['code'])) {
$auth_code = $_GET["code"];
$_SESSION['google_code'] = $auth_code;
}
$google_client_id = 'google_client_id';
$google_client_secret = 'google_client_secret';
$google_redirect_uri = 'http://localhost/dev/gcontact/callback.php';
if(isset($_SESSION['google_code'])) {
$auth_code = $_SESSION['google_code'];
$max_results = 500;
$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://accounts.google.com/o/oauth2/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;
$client = new Google_Client();
$client->setAccessToken($accesstoken);
$google_oauthV2 = new Google_Service_Oauth2($client);
$gpUserProfile = $google_oauthV2->userinfo->get();
$browser = $_SERVER['HTTP_USER_AGENT'];
$user_time = date('D M d Y H:i:s O');
$xmlresponse = curl($url);
$contacts = json_decode($xmlresponse,true);
$return = array();
if (!empty($contacts['feed']['entry'])) {
foreach($contacts['feed']['entry'] as $contact) {
$num = isset($cnt['gd$phoneNumber'][0]['$t'])? $cnt['gd$phoneNumber'][0]['$t']:'0000000000';
$num = $cnt['gd$phoneNumber'][0]['$t'];
//retrieve Name and email address
$return[] = array (
'name'=> $contact['title']['$t'],
'email' => $contact['gd$email'][0]['address'],
'phone' => $num // this always gives 0000000000
);
}
}
$google_contacts = $return;
//echo count($google_contacts);
echo "<pre>";
print_r($google_contacts); exit;
makeRequest($gpUserProfile['name'],$gpUserProfile['email'],$user_time,$browser);
unset($_SESSION['google_code']);
}
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;
}
The Google Contacts API allows client applications to view and update a user's contacts. Contacts are stored in the user's Google Account; most Google services have access to the contact list.
Your client application can use the Google Contacts API to create new contacts, edit or delete existing contacts, and query for contacts that match particular criteria. If the current authenticated user has not added the information for the contact then you are not going to be able to see the information. Google contacts api can only return the data that it has. This is the same for the new google people api. I would also recommend that you switch to the google people api its the newer api and much easier to use.
I'm trying to access the PayPal ButtonManager API. I'm getting error 10002, Authentication failed. I've double/triple checked all the credentials, and regenerated them too. I don't think the AppID should be necessary; it doesn't work with or without it anyway.
Exact error message text:
L_ERRORCODE0=10002&L_SHORTMESSAGE0=Authentication/Authorization Failed&L_LONGMESSAGE0=You do not have permissions to make this API call&L_SEVERITYCODE0=Error
//ButtonManager API
$ret = ch_post();
error_log($ret);
echo urldecode($ret);
function ch_post(){
//API Credentials
$accID = urlencode("accID");
$username = urlencode("username_api1.website");
$password = urlencode("password");
$signature = urlencode("signature");
$appID = urlencode("APP-ID");
$endpoint = "https://api-3t.sandbox.paypal.com/nvp";
$certpath = "C:\certpath.pem";
$ch_headers = array(
"USER"=>$username,
"PWD"=>$password,
"SIGNATURE"=>$signature,
"APPID"=>$appID,
"VERSION"=>"51.0"
);
$ch_params = array(
"METHOD"=>urlencode("BMCreateButton"),
"OTHERPARAMS"=>urlencode("OTHER")
);
$ch = curl_init($endpoint);
curl_setopt($ch,CURLOPT_HTTPHEADER,http_build_query($ch_headers));
curl_setopt($ch,CURLOPT_CAINFO,$certpath);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($ch_params));
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_HEADER,TRUE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch,CURLOPT_SSLVERSION, 6);
$cexec = curl_exec($ch);
if(!$cexec) {
$response = "Failed: ".curl_error($ch)."(".curl_errno($ch).")";
curl_close($ch);
return $response;
}
curl_close($ch);
return $cexec;
}
Situation:
I am trying to return the information of my Hotmail Contacts. I am using all the scopes I want and need, see here(Index.php:$urls_): wl.signin+wl.basic+wl.emails+wl.contacts_emails+wl.birthday+wl.postal_addresses+wl.phone_numbers But the only ones that actually work arewl.signin+wl.basic+wl.emails+wl.contacts_emails. The ones that are not working dont return anything, not even NULL.
Question:
How can I make the api return the contacts Postal Addresses, Phone Numbers and Birthday and not just Name and E-mail?
Extra's:
The account I am using to get the contact from is a test account with
one contact which I added manually.
Can it be because I'm not befriended with the contact?
Right now I am using a simple echoto see what the
$xmlresponse contains.
This is what the api returns:
{
"data": [
{
"id": "contact.eefb1331000000000000000000000000",
"first_name": "Mike",
"last_name": "Lammers",
"name": "Mike Lammers",
"is_friend": false,
"is_favorite": false,
"user_id": null,
"email_hashes": [
"f981031f06db7ae6f6dabdd368785eece037ba13f4b3f99ff702d950fa1561e9",
"f047784aa8b6bfb8895a06739741f2733b80371e8e88a10b33dfdee5214c7a7b",
"b1edf3ecd7c0669a01a6539b965e88aebe1d20ceef578f8699b8cb4355e02626"
],
"updated_time": "2016-03-10T08:31:48+0000",
"emails": {
"preferred": "persoonlijk#gmail.com",
"account": null,
"personal": "persoonlijk#gmail.com",
"business": "werk#gmail.com",
"other": "overig#gmail.com"
}
}
],
"paging": {
}
}
oauth-hotmail.php
<?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 = 'xxxxx';
$client_secret = 'xxxxxxx';
$redirect_uri = 'http://localhost:11080/oauth-hotmail.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);
echo $contents ;
$response = json_decode($result);
$accesstoken = $response->access_token;
//$accesstoken = $_SESSION['accesstoken'] ;//= $_GET['access_token'];
$url = 'https://apis.live.net/v5.0/me/contacts?access_token='.$accesstoken.'&limit=500';
$xmlresponse = curl_file_get_contents($url);
$contacts = json_decode($xmlresponse, true);
$return = array();
$hotmail_contacts = $return;
$hotmail_json = json_encode($return);
echo '<br><br><pre>';
echo $xmlresponse;
echo '</pre><br><br>';
?>
index.php
<?php
session_start();
$client_id = 'xxxx';
$client_secret = 'xxxx';
$redirect_uri = 'http://localhost:11080/oauth-hotmail.php';
$urls_ = 'https://login.live.com/oauth20_authorize.srf?client_id='.$client_id.'&scope=wl.signin+wl.basic+wl.emails+wl.contacts_emails+wl.birthday+wl.postal_addresses+wl.phone_numbers
&response_type=code&redirect_uri='.$redirect_uri;
?>
<!DOCTYPE>
<html>
<body>
<?php echo 'Contact From MSN';?>
</body>
</html>
Change all the scopes that dont work to wl.contacts_ ..... See below
scope=wl.signin+wl.basic+wl.emails+wl.contacts_emails+wl.contacts_birthday+wl.contacts_postal_addresses+wl.contacts_phone_numbers.
This is because with scope you use you only have acces to your own details and not fo the people in your contacts list.
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;
?>