Constant Contact : How to resolve error "JSON payload cannot be empty." - php

I want to use API of the constant contact and want to insert user Email Ids using PHP script while user register to the website.
I using following code for this.
$data['addresses'][0]['address_type'] = 'BUSINESS';
$data['addresses'][0]['city'] = 'Belleville';
$data['addresses'][0]['country_code'] = 'CA';
$data['addresses'][0]['line1'] = '47 Shawmut Ave.';
$data['addresses'][0]['line2'] = 'Suite 404';
$data['addresses'][0]['postal_code'] = '"K8b 5W6';
$data['addresses'][0]['state_code'] = 'ON';
$data['lists'][0]['id'] = "1554397204";
$data['cell_phone'] = "555-555-5555";
$data['company_name'] = "System Optimzations";
$data['confirmed'] = false;
$data['email_addresses'][0]['email_address'] = "username112#example.com";
$data['fax'] = "555-555-5555";
$data['first_name'] = "Manvendra";
$data['home_phone'] = "555-555-5555";
$data['job_title'] = "Systems Analyst 3";
$data['last_name'] = "Rajpurohit";
$data['prefix_name'] = "Mr.Martone";
$data['work_phone'] = "555-555-5555";
$jsonstring = json_encode($data);
// echo '<pre>'; print_r($data);
$posturl = "https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_OWNER&api_key=*******";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $posturl);
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
// curl_setopt($ch, CURLOPT_USERPWD, $authstr);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonstring);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json",'Authorization: Bearer *****
','Content-Length: ' . strlen($jsonstring)));
curl_setopt($ch, CURLOPT_HEADER, false); // Do not return headers
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // If you set this to 0, it will take you to a page with the http response
$response = curl_exec($ch);
if (FALSE === $response)
throw new Exception(curl_error($ch), curl_errno($ch));
echo '<pre>'; print_r($response); die;
curl_close($ch);
When I run this script then its showing following error.
[{"error_key":"json.payload.empty","error_message":"JSON payload cannot be empty."}]
Please help me that how can I resolve this error.

The actual problem is validation error,which is:-
[{"error_key":"json.max.length.violation","error_message":"#/prefix_name: Value exceeds maximum length of 4."}]
So what i did is changed:-
$data['prefix_name'] = "Mr.Martone";
To:-
$data['prefix_name'] = "tone"; // length need to be 4 or less only
And used this CURL request and data saved successfully.
<?php
$data['addresses'][0]['address_type'] = 'BUSINESS';
$data['addresses'][0]['city'] = 'Belleville';
$data['addresses'][0]['country_code'] = 'CA';
$data['addresses'][0]['line1'] = '47 Shawmut Ave.';
$data['addresses'][0]['line2'] = 'Suite 404';
$data['addresses'][0]['postal_code'] = '"K8b 5W6';
$data['addresses'][0]['state_code'] = 'ON';
$data['lists'][0]['id'] = "1554397204";
$data['cell_phone'] = "555-555-5555";
$data['company_name'] = "System Optimzations";
$data['confirmed'] = false;
$data['email_addresses'][0]['email_address'] = "username112#example.com";
$data['fax'] = "555-555-5555";
$data['first_name'] = "Manvendra";
$data['home_phone'] = "555-555-5555";
$data['job_title'] = "Systems Analyst 3";
$data['last_name'] = "Rajpurohit";
$data['prefix_name'] = "tone";
$data['work_phone'] = "555-555-5555";
$jsonstring = json_encode($data);
//echo '<pre>'; print_r($data);
$posturl = "https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_OWNER&api_key=*******";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonstring);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json",'Authorization: Bearer ******')); //check change here
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// this need to be 1
$response = curl_exec($ch);
if (FALSE === $response)
throw new Exception(curl_error($ch), curl_errno($ch));
echo '<pre>'; print_r($response); die;
curl_close($ch);
Output i got is:-
{
"id": "1519826644",
"status": "ACTIVE",
"fax": "555-555-5555",
"addresses": [{
"id": "9ac8fd40-c2b2-11e7-aa57-d4ae5284344f",
"line1": "47 Shawmut Ave.",
"line2": "Suite 404",
"line3": "",
"city": "Belleville",
"address_type": "BUSINESS",
"state_code": "ON",
"state": "Ontario",
"country_code": "ca",
"postal_code": "\"K8b",
"sub_postal_code": "5W6"
}],
"notes": [],
"confirmed": false,
"lists": [{
"id": "1554397204",
"status": "ACTIVE"
}],
"source": "API",
"email_addresses": [{
"id": "9a914b70-c2b2-11e7-aa57-d4ae5284344f",
"status": "ACTIVE",
"confirm_status": "NO_CONFIRMATION_REQUIRED",
"opt_in_source": "ACTION_BY_OWNER",
"opt_in_date": "2017-11-06T05:23:21.000Z",
"email_address": "username23333#example.com"
}],
"prefix_name": "tone",
"first_name": "Manvendra",
"middle_name": "",
"last_name": "Rajpurohit",
"job_title": "Systems Analyst 3",
"company_name": "System Optimzations",
"home_phone": "555-555-5555",
"work_phone": "555-555-5555",
"cell_phone": "555-555-5555",
"custom_fields": [],
"created_date": "2017-11-06T05:23:20.000Z",
"modified_date": "2017-11-06T05:23:20.000Z",
"source_details": "Web Devloping"
}

Related

Accessing data sent to callback URL

Following a successful response from an API Request to M-PESA where the following JSON code is printed:
{ "MerchantRequestID":"2690XXXXXXX", "CheckoutRequestID":"xx_XX_2779308581984", "ResponseCode": "0", "ResponseDescription":"Success. Request accepted for processing", "CustomerMessage":"Success. Request accepted for processing" }
I would like to access the data sent to my callback url: https://xxxxxxxxxxxxxxxxx.ngrok.io/processL/transact.php . Despite expecting the code to change accordingly upon cancellation or payment, nothing happens although when I inspect the response on the ngrok local tunnel online interface, I see my expected results only that I do not know how to acquire and generate some action with them. Below is the code making the API Request and the results which I expect to be sent to my call back url but in vain.
<!-- transact.php -->
<?php
if (isset($_POST['submit'])) {
$Passkey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$Amount= $_POST['amt'];
$BusinessShortCode = '174379';
$PartyA =$_POST['phone'];
$AccountReference =$_POST['name'];
$TransactionDesc = 'test';
$Timestamp =date('YmdHis');
$Password = base64_encode($BusinessShortCode.$Passkey.$Timestamp);
$headers=['Content-Type:application/json; charset=utf8'];
$initiate_url='https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest';
$callBackURL ='https://xxxxxxxxxxxxxxxxx.ngrok.io/processL/transact.php';
function accessToken() {
$ConsumerKey = 'ubYsxxxxxxxxxxxxx';
$ConsumerSecret = 'xxxxxxxxxxxxx';
$credentials = base64_encode($ConsumerKey.":".$ConsumerSecret);
$url = "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Basic ".$credentials,"Content-Type:application/json"));
curl_setopt($curl, CURLOPT_HEADER, false);
// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
$access_token=json_decode($curl_response);
curl_close($curl);
return $access_token->access_token;
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $initiate_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:Bearer '.accessToken()));
$curl_post_data = array(
'BusinessShortCode' =>$BusinessShortCode,
'Password' => $Password,
'Timestamp' => $Timestamp,
'TransactionType' => 'CustomerPayBillOnline',
'Amount' => $Amount,
'PartyA' => $PartyA,
'PartyB' => $BusinessShortCode,
'PhoneNumber' => $PartyA,
'CallBackURL' => $callBackURL,
'AccountReference' => $AccountReference,
'TransactionDesc' => $TransactionDesc
);
$data_string = json_encode($curl_post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$curl_response = curl_exec($curl);
print_r($curl_response."<br>");
}
Upon acceptance of payment:
{
"Body": {
"stkCallback": {
"MerchantRequestID": "xxxxxxxxxxxxx",
"CheckoutRequestID": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"ResultCode": 0,
"ResultDesc": "The service request is processed successfully.",
"CallbackMetadata": {
"Item": [
{
"Name": "Amount",
"Value": 1
},
{
"Name": "MpesaReceiptNumber",
"Value": "xxxxxxxxxxxxx"
},
{
"Name": "Balance"
},
{
"Name": "TransactionDate",
"Value": 20210927101413
},
{
"Name": "PhoneNumber",
"Value": xxxxxxxxxxxxx
}
]
}
}
}
};
When payment is cancelled:
{
"Body": {
"stkCallback": {
"MerchantRequestID": "xxxxxxxxxxxxx",
"CheckoutRequestID": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"ResultCode": 1032,
"ResultDesc": "Request cancelled by user"
}
}
};
I need some guidance.
The documentation for json_decode() is your friend. If you pass true as the second argument then it returns your JSON data as associative arrays.
$access_token=json_decode($curl_response, true);
// Check the response value
$result = $access_token['Body']['stkCallback']['ResultCode'] ?? null;
if ($result == 0) {
// Success
}

Cumulocity: Call all event by deviceId and datetime

Hi all i using cumulocity api with php , is all ready.
I have problem with get event/events by deviceId.
$url = 'https://*********.iot.a1.digital/event/events?dateFrom=' . $date . 'T' . $timeNew . '.000Z&pageSize=1000&type=lwm2m_log';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
// curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Acept: application/vnd.com.nsn.cumulocity.operation+json',
));
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
My url is
https://********_live.iot.a1.digital/event/events?dateFrom=2020-08-28T14:02:07.000Z&pageSize=1000&type=lwm2m_log&id=1234567
but return data is all event for all device.
answer
"events": [
{
"creationTime": "2020-12-07T09:49:13.879+01:00",
"source": {
"name": "860922049984564",
"self": "https://t14580169.iot.a1.digital/inventory/managedObjects/1973856",
"id": "1973856"
},
"type": "lwm2m_log",
"self": "https://t14580169.iot.a1.digital/event/events/13113138",
"time": "2020-12-07T09:49:13.874+01:00",
"text": "Registration update: RegistrationUpdate [registrationId=ivWv8hUHh2, identity=Identity /80.75.32.47:24140[unsecure], lifeTimeInSec=null, smsNumber=null, bindingMode=null, objectLinks=[</31101/0>, </5/0>, </3/0>, </6/0>]]\nUpdated registration: Registration [registrationDate=Mon Nov 23 09:19:00 CET 2020, identity=Identity /80.75.32.47:24140[unsecure], lifeTimeInSec=360, smsNumber=null, lwM2mVersion=1.0, bindingMode=U, endpoint=860922049984564, registrationId=ivWv8hUHh2, objectLinks=[</31101/0>, </5/0>, </3/0>, </6/0>], lastUpdate=Mon Dec 07 09:49:13 CET 2020]",
"id": "13113138"
}
]
How to create url get by deviceId
To query events (or other data types) by source device ID, please use the query parameter "source" instead. See https://cumulocity.com/guides/reference/events/#events-api
Your query should look like this:
https://********_live.iot.a1.digital/event/events?dateFrom=2020-08-28T14:02:07.000Z&pageSize=1000&type=lwm2m_log&source=1234567

Why am I getting ' BAD_REQUEST Unprocessable JSON'?

I'm trying to make a call to a REST-API but it only returns 'BAD_REQUEST Unprocessable JSON'.
I've tried rewriting the input as the example I'm following, but it just gives me the same error.
This is the code I'm using for sending the request:
function callAPI($method, $url, $data)
{
$curl = curl_init();
$username = 'PK10008_e8f77aebdb0a';
$password = 'rwBmthNFK8JTbIL7';
switch($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
}
//Options
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if(!$result)
{
die("Connection Failure");
}
curl_close($curl);
return $result;
}
And this is the code I'm using to create and send the information:
$data_array = array(
'purchase_country'=> 'SE',
'purchase_currency'=> 'SEK',
'locale'=> 'sv-SE',
'order_amount'=> 10,
'order_tax_amount'=> 0,
'order_lines' => array(
'type'=> 'physical',
'reference'=> '19-402',
'name'=> 'Test',
'quantity'=> 1,
'unit_price'=> 10,
'tax_rate'=> 0,
'total_amount'=> 10,
'total_discount_amount'=> 0,
'total_tax_amount'=> 0
)
);
$make_call = callAPI('POST', 'https://api.playground.klarna.com/payments/v1/sessions', json_encode($data_array));
$response = json_decode($make_call, true);
And this is the example:
POST /payments/v1/sessions
Authorization: Basic pwhcueUff0MmwLShJiBE9JHA==
Content-Type: application/json
{
"purchase_country": "SE",
"purchase_currency": "SEK",
"locale": "sv-SE",
"order_amount": 10,
"order_tax_amount": 0,
"order_lines": [{
"type": "physical",
"reference": "19-402",
"name": "Battery Power Pack",
"quantity": 1,
"unit_price": 10,
"tax_rate": 0,
"total_amount": 10,
"total_discount_amount": 0,
"total_tax_amount": 0
}]
}
The result I'm getting is this:
'BAD_REQUEST
Unprocessable JSON
1734c472-c9cb-4022-b469-a8231c7abeec'
But I should be getting some thing like this:
HTTP/1.1 200 OK
Content-Type: application/json
{
"session_id": "068df369-13a7-4d47-a564-62f8408bb760",
"client_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjAwMDAwMDAwMDAtMDAwMDAtMDAwMC0wMDAwMDAwMC0wMDAwIiwidXJsIjoiaHR0cHM6Ly9jcmVkaXQtZXUua2xhcm5hLmNvbSJ9.A_rHWMSXQN2NRNGYTREBTkGwYwtm-sulkSDMvlJL87M",
"payment_method_categories": [{
"identifier": "pay_later"
"name" : "Pay later.",
"asset_urls" : {
"descriptive" : "https://cdn.klarna.com/1.0/shared/image/generic/badge/en_us/pay_later/descriptive/pink.svg",
"standard" : "https://cdn.klarna.com/1.0/shared/image/generic/badge/en_us/pay_later/standard/pink.svg"
}
}]
}
The issue I was having was how json_encode creates encodes arrays. Instead of this: "example":[{
"ex": "ex"
}]
It did this:
"example":{
"ex": "ex"
}
Which in turn made the json unreadable to the end API

Joint two or more json files

I have a problem need to join or merge two or more json file..
so far here's my code:
//first
$url1="https://www.zopim.com/api/v2/chats";
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url1);
curl_setopt($ch1, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$output1 = curl_exec($ch1);
$info1 = curl_getinfo($ch1);
curl_close($ch1);
$chats1 = json_decode($output1,true);
//second
$url2="https://www.zopim.com/api/v2/chats?page=2";
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $url2);
curl_setopt($ch2, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch2, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
$output2 = curl_exec($ch2);
$info2 = curl_getinfo($ch2);
curl_close($ch2);
$chats2 = json_decode($output2,true);
$r = [];
if(is_array($chats1) && is_array($chats2))
{
foreach($chats1 as $key => $array)
{
$r[$key] = array_merge($chats2[$key], $array);
}
}
else
{
echo 'problem with json';
}
echo json_encode($r, JSON_UNESCAPED_SLASHES);
but i encounter an error:
here's number 44 error line:
hopefully you can help me or do you have a much better code or login for this one.. like using foreach...
I also want to make the link auto generated by number like ?page=1, ?page=2 and so on...
Here's my json:
json1
and json2:
$finalArray = [];
$finalArray[] = json_decode($json1,true);
$finalArray[] = json_decode($json2,true);
$mergedJSON = json_encode($finalArray);
For same array structure you can use
array_merge($array1, $array2)
method & then use
json_encode().
Example:
$mergedArray = array_merge($array1, $array2);
$mergedJSON = json_encode($mergedArray);
I have create two small json for join it ! Check below,
Json - 1
{
"chats": [
{
"comment": null,
"triggered_response": true,
"visitor": {
"phone": ""
},
"session": {
"city": "Moncton",
"end_date": "2017-07-03",
"ip": "99.240.22.84"
},
"duration": "32",
"agent_names": {
"name": "test"
}
}
]
}
Json - 2
{
"chats": [
{
"comment": null,
"triggered_response": true,
"visitor": {
"phone": ""
},
"session": {
"city": "Moncton1",
"end_date": "2017-08-03",
"ip": "99.240.22.85"
},
"duration": "321",
"agent_names": {
"name": "test1"
}
}
]
}
Now i join that using array_merge_recursive.
PHP
// $json is first json encoded string same as $json1 is second encoded string.
$merge_array = array_merge_recursive(json_decode($json,true),json_decode($json1,true));
Json encoded Output
{
"chats": [
{
"comment": null,
"triggered_response": true,
"visitor": {
"phone": ""
},
"session": {
"city": "Moncton",
"end_date": "2017-07-03",
"ip": "99.240.22.84"
},
"duration": "32",
"agent_names": {
"name": "test"
}
},
{
"comment": null,
"triggered_response": true,
"visitor": {
"phone": ""
},
"session": {
"city": "Moncton1",
"end_date": "2017-08-03",
"ip": "99.240.22.85"
},
"duration": "321",
"agent_names": {
"name": "test1"
}
}
]
}
If your both array structure same Here you pass $chats2[$key] as string on array_merge(); so error occurred
$res_arr = array_merge($chats2,$chats1);
array_values($res_arr);
$r = [];
if(!empty($res_arr))
{
foreach($res_arr as $key => $array)
{
$r[$key] = $array;
}
}
else
{
echo 'problem with json';
}
echo json_encode($r);
You can do this:
$user = array();
$user[] = json_decode($json1,true);
$user[] = json_decode($json2,true);
$json_merge = json_encode($user);

Outlook REST API not returning all the data it should

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.

Categories