Display JSON response using PHP from URL - php

I have a feed from a parcel tracking service that i am trying to integrate into my site. I have a url which allows me to put the tracking number at the end and get a json response. I have multiple objects from this which include some static info such as the senders address and some info which I will need to use a foreach for like the tracking progress.
I believe I have got the string okay however I am not sure how I am meant to display the info.
This is what I have so far:
Example URL:
domain.com/REST_Service/webservice/consignee/SelfshipService.svc/web/Tracking/84941354665
URL Returns:
{
"Agent": null,
"Consignee": {
"Address1": "25 HEATHFIELD ROAD",
"Address2": "SHOLING",
"Address3": "",
"Code": null,
"Company": "ERIK HANSON",
"Country": "GREAT BRITAIN",
"Dept": "",
"Email": "ERIK.HANSON66#GOOGLEMAIL.COM",
"Name": "",
"Phone": "07770320490",
"Postcode": "SO19 1DL",
"State": "HANTS",
"Town": "SOUTHAMPTON"
},
"CrossIdx": "",
"Error": null,
"NonDel": null,
"POD": {
"Date": "13 Jul 2016",
"Status": "Harnett",
"Time": "09:17"
},
"Pieces": 1,
"PosErr": 0,
"Tracks": [
{
"Date": "13 Jul 2016",
"Status": "Out for delivery",
"Time": "07:10"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:24"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:22"
},
{
"Date": "12 Jul 2016",
"Status": "Arrived At Ryton",
"Time": "22:12"
},
{
"Date": "12 Jul 2016",
"Status": "Preparing for despatch",
"Time": "14:00"
},
{
"Date": "12 Jul 2016",
"Status": "Scanned into OCS HEATHROW LONDON",
"Time": "13:59"
},
{
"Date": "12 Jul 2016",
"Status": "Consignment details verified",
"Time": "13:59"
},
{
"Date": "14 Jun 2016",
"Status": "Shipment prepared by customer",
"Time": "11:20"
},
{
"Date": "02 Jan 2003",
"Status": "Collected from Customer",
"Time": "09:32"
}
],
"Weight": 7
}
Current PHP:
//set tracking url
$url = "http://www.ocscourier.co.uk/REST_Service/webservice/consignee/SelfshipService.svc/web/Tracking/84941354665";
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://www.ocscourier.co.uk/REST_Service/webservice/consignee/SelfshipService.svc/web/Tracking/84941354665");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
//call api
//$json = file_get_contents($url);
$json = json_decode($output);
$Address1 = $json->results[0]->Consignee->Address1;
$Address2 = $json->results[0]->Consignee->Address2;
echo "Address 1: " . $Address1 . ", Address 2: " . $Address2;

Your json string doesn't have any results key (so I'm not sure why you trying to access results[0].
You can just use
$Address1 = $json->Consignee->Address1;
$Address2 = $json->Consignee->Address2;
Check this code:
$s = <<< END
{
"Agent": null,
"Consignee": {
"Address1": "25 HEATHFIELD ROAD",
"Address2": "SHOLING",
"Address3": "",
"Code": null,
"Company": "ERIK HANSON",
"Country": "GREAT BRITAIN",
"Dept": "",
"Email": "ERIK.HANSON66#GOOGLEMAIL.COM",
"Name": "",
"Phone": "07770320490",
"Postcode": "SO19 1DL",
"State": "HANTS",
"Town": "SOUTHAMPTON"
},
"CrossIdx": "",
"Error": null,
"NonDel": null,
"POD": {
"Date": "13 Jul 2016",
"Status": "Harnett",
"Time": "09:17"
},
"Pieces": 1,
"PosErr": 0,
"Tracks": [
{
"Date": "13 Jul 2016",
"Status": "Out for delivery",
"Time": "07:10"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:24"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:22"
},
{
"Date": "12 Jul 2016",
"Status": "Arrived At Ryton",
"Time": "22:12"
},
{
"Date": "12 Jul 2016",
"Status": "Preparing for despatch",
"Time": "14:00"
},
{
"Date": "12 Jul 2016",
"Status": "Scanned into OCS HEATHROW LONDON",
"Time": "13:59"
},
{
"Date": "12 Jul 2016",
"Status": "Consignment details verified",
"Time": "13:59"
},
{
"Date": "14 Jun 2016",
"Status": "Shipment prepared by customer",
"Time": "11:20"
},
{
"Date": "02 Jan 2003",
"Status": "Collected from Customer",
"Time": "09:32"
}
],
"Weight": 7
}
END;
$json = json_decode($s);
$Address1 = $json->Consignee->Address1;
$Address2 = $json->Consignee->Address2;
echo "Address 1: " . $Address1 . ", Address 2: " . $Address2;
Here is the output:
Address 1: 25 HEATHFIELD ROAD, Address 2: SHOLING

Below is json string in $json variable and access with RecursiveIteratorIterator
Example
$json='{
"Agent": null,
"Consignee": {
"Address1": "25 HEATHFIELD ROAD",
"Address2": "SHOLING",
"Address3": "",
"Code": null,
"Company": "ERIK HANSON",
"Country": "GREAT BRITAIN",
"Dept": "",
"Email": "ERIK.HANSON66#GOOGLEMAIL.COM",
"Name": "",
"Phone": "07770320490",
"Postcode": "SO19 1DL",
"State": "HANTS",
"Town": "SOUTHAMPTON"
},
"CrossIdx": "",
"Error": null,
"NonDel": null,
"POD": {
"Date": "13 Jul 2016",
"Status": "Harnett",
"Time": "09:17"
},
"Pieces": 1,
"PosErr": 0,
"Tracks": [
{
"Date": "13 Jul 2016",
"Status": "Out for delivery",
"Time": "07:10"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:24"
},
{
"Date": "13 Jul 2016",
"Status": "At Delivery location Portsmouth",
"Time": "02:22"
},
{
"Date": "12 Jul 2016",
"Status": "Arrived At Ryton",
"Time": "22:12"
},
{
"Date": "12 Jul 2016",
"Status": "Preparing for despatch",
"Time": "14:00"
},
{
"Date": "12 Jul 2016",
"Status": "Scanned into OCS HEATHROW LONDON",
"Time": "13:59"
},
{
"Date": "12 Jul 2016",
"Status": "Consignment details verified",
"Time": "13:59"
},
{
"Date": "14 Jun 2016",
"Status": "Shipment prepared by customer",
"Time": "11:20"
},
{
"Date": "02 Jan 2003",
"Status": "Collected from Customer",
"Time": "09:32"
}
],
"Weight": 7
}';
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($json, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
echo "$key:\n</br>";
} else {
echo "$key => $val\n";
}
}
OUTPUT:
Agent => Consignee:
Address1 => 25 HEATHFIELD ROAD Address2 => SHOLING Address3 => Code => Company => ERIK HANSON Country => GREAT BRITAIN Dept => Email => ERIK.HANSON66#GOOGLEMAIL.COM Name => Phone => 07770320490 Postcode => SO19 1DL State => HANTS Town => SOUTHAMPTON CrossIdx => Error => NonDel => POD:
Date => 13 Jul 2016 Status => Harnett Time => 09:17 Pieces => 1 PosErr => 0 Tracks:
0:
Date => 13 Jul 2016 Status => Out for delivery Time => 07:10 1:
Date => 13 Jul 2016 Status => At Delivery location Portsmouth Time => 02:24 2:
Date => 13 Jul 2016 Status => At Delivery location Portsmouth Time => 02:22 3:
Date => 12 Jul 2016 Status => Arrived At Ryton Time => 22:12 4:
Date => 12 Jul 2016 Status => Preparing for despatch Time => 14:00 5:
Date => 12 Jul 2016 Status => Scanned into OCS HEATHROW LONDON Time => 13:59 6:
Date => 12 Jul 2016 Status => Consignment details verified Time => 13:59 7:
Date => 14 Jun 2016 Status => Shipment prepared by customer Time => 11:20 8:
Date => 02 Jan 2003 Status => Collected from Customer Time => 09:32 Weight => 7

Related

Group the objects based on object key and value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
My present objects
{
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021"
"type": "Public",
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021"
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021"
"type": "Public"
}
}
I want the grouped object like
{
date:2021-01-05,
public:{
{"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021"
"type": "Public",
},
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021"
"type": "Public",
},
Private:{
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
}
},
{
date:2021-01-05,
public:{
{"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-05",
"date": "January 05, 2021"
"type": "Public",
},
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-05",
"date": "January 05, 2021"
"type": "Public",
},
Private:{
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-05",
"date": "January 05, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"compare_date": "2021-01-0",
"date": "January 05, 2021",
"type": "Private"
},
}
}
Unfortunately, the input data you provided is not valid JSON. There were commas missing or at the wrong place and the outer curly brackets should have been square ones. Here is a "corrected" version and a way to produce the output you desired as "closely as possible":
$in='[
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-04 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021",
"type": "Public"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-05",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Private"
},
{
"_id": "5ff3e51c8bb6cc1e564075c3",
"user_id": "210105093340",
"account_status": "Active",
"profile_pic": "1609837105.891015.jpeg",
"cd": "2021-01-05 09:33:40",
"compare_date": "2021-01-06",
"date": "January 06, 2021",
"type": "Public"
}
]';
$out=array_reduce(json_decode($in), function($a,$c){
$a[substr($c->cd,0,10)][$c->type][]=[
"_id"=>$c->_id,
"user_id"=>$c->user_id,
"account_status"=>$c->account_status,
"compare_date"=>$c->compare_date,
"date"=>$c->date,
"type"=>$c->type ];
return $a;}, [] );
array_walk($out,function($arr,$key) use(&$res) {
$arr['Date']=$key;
array_walk($arr, function($ar,$k) use($key,&$res) {
$res[$key][$k]=$ar;
});
});
foreach ($res as $el) $result[]=$el;
print_r($result);
You can find a working demo here: https://rextester.com/BQWU57249
This is the output:
array
(
[0] => Array
(
[Public] => Array
(
[0] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-05
[date] => January 06, 2021
[type] => Public
)
)
[Date] => 2021-01-04
)
[1] => Array
(
[Private] => Array
(
[0] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-05
[date] => January 06, 2021
[type] => Private
)
[1] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-06
[date] => January 06, 2021
[type] => Private
)
)
[Public] => Array
(
[0] => Array
(
[_id] => 5ff3e51c8bb6cc1e564075c3
[user_id] => 210105093340
[account_status] => Active
[compare_date] => 2021-01-06
[date] => January 06, 2021
[type] => Public
)
)
[Date] => 2021-01-05
)
)
Assuming you want them grouped by date, and then further by the type of the account, you could do something like this.
A relatively simple foreach loop should do the trick, considering you have transformed your json into a array of arrays.
$filteredAccounts = [];
foreach ($accounts as $account) {
$date = new \DateTime($account['date']);
$filteredAccounts[$date->format('Y-m-d')][$account['type']][] = $account;
}
// Then you build your final array you want.
$groupedAccounts = [];
foreach ($filteredAccounts as $date => $filteredAccount) {
$filteredAccount['Date'] = $date;
$groupedAccounts[] = $filteredAccount;
}
var_dump($groupedAccounts);
But as others have mentioned your question doesn't really have anything to do with Laravel, but regardless this should help you along the way if you wanted it in a raw way.

Facebook Access Token: MACHINE_ID

I did a little PHP script that uses this principle: https://developers.facebook.com/docs/facebook-login/access-tokens/expiration-and-extension#long-via-code
Unfortunately when I send the CODE to the user and try to HTTPrequest (javascript), the page that should give me the MACHINE_ID responds with a random, changing string.
I guess HTTPrequest is called basically from the server, not the client. When I use direct URL connection I get the same string every time, but I cannot use the data, because it is just a web page, not a response.
Shouldn't the MACHINE_ID always be the same (for a certain device/browser session)?
{
"session_key": "5.xLL8XelWvN17yQ.1530200602.46-100004938759387",
"uid": 100004938759387,
"secret": "62818f28dadf1e5fdea17c70cc42b4e5",
"access_token": "EAAAAUaZA8jlABAF8vs7Xp3bYhFrUjGZC7IJVIkBtsxGEbwjIhy7PtysyYCZB4ZBZAlXNvxSbsYZA9giZC8ZBhWZCpaTuYxGZBhGSo0sb1r5R8XPjmxbSHZCjuQgxjcgPlbVwsAVdboRHEyyc9cN6FfPFz3Jv9cB8xguVk4gyRCD6JQ5fhHBXxdykQwl",
"machine_id": "GgI1W1EjtchM9UoB1IiILZke",
"session_cookies": [{
"name": "c_user",
"value": "100004938759387",
"expires": "Fri, 28 Jun 2019 15:43:22 GMT",
"expires_timestamp": 1561736602,
"domain": ".facebook.com",
"path": "\/",
"secure": true
}, {
"name": "xs",
"value": "46:xLL8XelWvN17yQ:2:1530200602:15323:8134",
"expires": "Fri, 28 Jun 2019 15:43:22 GMT",
"expires_timestamp": 1561736602,
"domain": ".facebook.com",
"path": "\/",
"secure": true,
"httponly": true
}, {
"name": "fr",
"value": "0LpS1kk8BIpMZ6ZnX.AWUXzwpmVcljO_CD31ozKSBgHno.BYGKXA.3G.AAA.0.0.BbNQIa.AWUgcG86",
"expires": "Fri, 28 Jun 2019 15:43:22 GMT",
"expires_timestamp": 1561736602,
"domain": ".facebook.com",
"path": "\/",
"secure": true,
"httponly": true
}, {
"name": "datr",
"value": "GgI1W1EjtchM9UoB1IiILZke",
"expires": "Sat, 27 Jun 2020 15:43:22 GMT",
"expires_timestamp": 1593272602,
"domain": ".facebook.com",
"path": "\/",
"secure": true,
"httponly": true
}],
"confirmed": true,
"identifier": "Christian.palele",
"user_storage_key": "7086edcf13fb66e64be979ea719e8cd24d658ed3bce735e4f871dc69eda26409"
}

how to get email and display name from oneall API?

{
"response": {
"request": {
"date": "Tue, 13 Sep 2011 11:24:28 +0200",
"resource": "/users/2cec711d-ca14-4472-98c8-ca74432bc2d3.json",
"status": {
"flag": "success",
"code": 200
}
},
"result": {
"data": {
"user": {
"user_token": "2cec711d-ca14-4472-98c8-ca74432bc2d3",
"date_creation": "Tue, 1 Sep 2011 11:01:12 +0200",
"date_last_login": "Tue, 13 Sep 2011 01:05:07 +0200",
"num_logins": "64",
"identities": [{
"identity_token": "cd3bd13b-b393-4d6c-a7f6-950b4c47938f",
"provider": "twitter",
"id": "http://twitter.com/ExampleUser",
"displayName": "Firstname Lastname",
"name": {
"formatted": "Firstname Lastname"
},
"gender": "male",
"utcOffset": "2:00"
}, {
"identity_token": "3ab5257b-ba2b-4242-a7f6-950b4c47938f",
"provider": "facebook",
"id": "http://www.facebook.com/profile.php?id=1046121518",
"displayName": "Firstname Lastname",
"name": {
"formatted": "Firstname Lastname",
"givenName": "Firstname",
"familyName": "Lastname"
},
"gender": "male",
"birthday": "01/01/1980",
"utcOffset": "2:00",
"emails": [{
"value": "email#example.com",
"is_verified": "true"
}]
}]
}
}
}
}
}
The above code is Result: the code returned by the ONEALL API
I am using oneall api for my website social login ,after i login i requested user details by get method using user token, the values like last login got by using $data->user->date_last_login, but i am unable to get email and dispalyname how can i do that one , i didn't wrote all the code i gave just sample, hope this will help for others also
http://docs.oneall.com/api/resources/users/read-user-details/
If you follow the object structure, you can get them as:
$data = json_decode($json);
$displayName = $data->response->result->data->user->identities[1]->displayName;
$email = $data->response->result->data->user->identities[1]->emails[0]->value;
Update:
If you are using intermediate var for response data, then just shrink the chain:
$json = json_decode($result_json);
//extract response data
$data = $json->response->result->data;
$displayName = $data->user->identities[1]->displayName;
$email = $data->user->identities[1]->emails[0]->value;
This will give you:
Firstname Lastname
email#example.com
Here we go complete code, based on your JSON object:
<?php
$result_json = '
{
"response": {
"request": {
"date": "Tue, 13 Sep 2011 11:24:28 +0200",
"resource": "/users/2cec711d-ca14-4472-98c8-ca74432bc2d3.json",
"status": {
"flag": "success",
"code": 200
}
},
"result": {
"data": {
"user": {
"user_token": "2cec711d-ca14-4472-98c8-ca74432bc2d3",
"date_creation": "Tue, 1 Sep 2011 11:01:12 +0200",
"date_last_login": "Tue, 13 Sep 2011 01:05:07 +0200",
"num_logins": "64",
"identities": [{
"identity_token": "cd3bd13b-b393-4d6c-a7f6-950b4c47938f",
"provider": "twitter",
"id": "http://twitter.com/ExampleUser",
"displayName": "Firstname Lastname",
"name": {
"formatted": "Firstname Lastname"
},
"gender": "male",
"utcOffset": "2:00"
}, {
"identity_token": "3ab5257b-ba2b-4242-a7f6-950b4c47938f",
"provider": "facebook",
"id": "http://www.facebook.com/profile.php?id=1046121518",
"displayName": "Firstname Lastname",
"name": {
"formatted": "Firstname Lastname",
"givenName": "Firstname",
"familyName": "Lastname"
},
"gender": "male",
"birthday": "01/01/1980",
"utcOffset": "2:00",
"emails": [{
"value": "email#example.com",
"is_verified": "true"
}]
}]
}
}
}
}
}';
$json = json_decode($result_json);
$data = $json->response->result->data;
echo $displayName = $data->user->identities[1]->displayName;
echo '<br>';
echo $email = $data->user->identities[1]->emails[0]->value;
?>

how can i read a gmail subject with php api?

in witch mode a can read the email subject with php api ?
i do this code
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Gmail($client);
// Print the labels in the user's account.
$user = 'me';
$results = $service->users_messages->listUsersMessages($user);
foreach($results as $mail){
$optParamsGet['format'] = 'metadata'; // Display message in payload
$message = $service->users_messages->get($user, $mail['id'],$optParamsGet);
$objMessage = $message->getPayload();
print_r($objMessage[18]['value']);
}
i find very difficult use this api with php because there isn't any documentation about the function.
where i can find the comple documentation?
(for example i found getPayload function because i see some example here)
thanks
If you have a look at a bare bones response you get when accessing the API with regular http-requests, I think a lot of things will be more clear.
I will list my latest message, and then get the message, and have a look at the response:
Request
maxResults = 1
GET https://www.googleapis.com/gmail/v1/users/me/messages?maxResults=1
Response
{
"messages": [
{
"id": "150791be31ee4e7b",
"threadId": "150791be31ee4e7b"
}
],
"nextPageToken": "05858850367051193217",
"resultSizeEstimate": 2
}
Request
GET https://www.googleapis.com/gmail/v1/users/me/messages/150791be31ee4e7b
Response
{
"id": "150791be31ee4e7b",
"threadId": "150791be31ee4e7b",
"labelIds": [
"INBOX",
"CATEGORY_SOCIAL",
"UNREAD"
],
"snippet": "Infinite Elgintensity has uploaded Gym Idiots - The Smith Machine's Retarded Cousin I have no",
"historyId": "623043",
"internalDate": "1445140881000",
"payload": {
"mimeType": "multipart/alternative",
"filename": "",
"headers": [
{
"name": "Delivered-To",
"value": "emtholin#gmail.com"
},
{
"name": "Received",
"value": "by 10.28.188.5 with SMTP id m5csp628249wmf; Sat, 17 Oct 2015 21:01:22 -0700 (PDT)"
},
{
"name": "X-Received",
"value": "by 10.182.28.74 with SMTP id z10mr15179312obg.80.1445140882358; Sat, 17 Oct 2015 21:01:22 -0700 (PDT)"
},
{
"name": "Return-Path",
"value": "<3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu#youtube-subscriptions.bounces.google.com>"
},
{
"name": "Received",
"value": "from mail-ob0-x245.google.com (mail-ob0-x245.google.com. [2607:f8b0:4003:c01::245]) by mx.google.com with ESMTPS id l8si12292524oej.1.2015.10.17.21.01.22 for <emtholin#gmail.com> (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Oct 2015 21:01:22 -0700 (PDT)"
},
{
"name": "Received-SPF",
"value": "pass (google.com: domain of 3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu#youtube-subscriptions.bounces.google.com designates 2607:f8b0:4003:c01::245 as permitted sender) client-ip=2607:f8b0:4003:c01::245;"
},
{
"name": "Authentication-Results",
"value": "mx.google.com; spf=pass (google.com: domain of 3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu#youtube-subscriptions.bounces.google.com designates 2607:f8b0:4003:c01::245 as permitted sender) smtp.mailfrom=3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu#youtube-subscriptions.bounces.google.com; dkim=pass header.i=#youtube.com; dmarc=pass (p=QUARANTINE dis=NONE) header.from=youtube.com"
},
{
"name": "Received",
"value": "by mail-ob0-x245.google.com with SMTP id da8so130023858obb.1 for <emtholin#gmail.com>; Sat, 17 Oct 2015 21:01:22 -0700 (PDT)"
},
{
"name": "X-Received",
"value": "by 10.107.148.204 with SMTP id w195mr21311032iod.15.1445140881929; Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
},
{
"name": "X-Received",
"value": "by 10.140.238.83 with SMTP id j80mr20061780qhc.2.1445140881732; Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
},
{
"name": "Return-Path",
"value": "<3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu#youtube-subscriptions.bounces.google.com>"
},
{
"name": "Received",
"value": "from mail-pa0-f72.google.com (mail-pa0-f72.google.com. [209.85.220.72]) by gmr-mx.google.com with ESMTPS id wl2si2608966pab.1.2015.10.17.21.01.21 for <thulle3-0107#pages.plusgoogle.com> (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
},
{
"name": "Received-SPF",
"value": "pass (google.com: domain of 3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu#youtube-subscriptions.bounces.google.com designates 209.85.220.72 as permitted sender) client-ip=209.85.220.72;"
},
{
"name": "Received",
"value": "by mail-pa0-f72.google.com with SMTP id da3so25pad.2 for <thulle3-0107#pages.plusgoogle.com>; Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
},
{
"name": "DKIM-Signature",
"value": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=youtube.com; s=20120806; h=mime-version:message-id:date:subject:from:to:content-type; bh=dxXkvZ79qLn59RVfXz4QAs5AW2+MyxrsrN55S+jKtK0=; b=mrrriloHJkEy4TT3wpzhEO1nvinpewLWHHExGGprhWlUU+5ME9M8w3xlaTeDcm0V0y QYDbH6TTG2rc0vwiLuZ46Dgm3rDC0brbg/Bdh3TQRUBneOTdAW4oMMs48DSIBbbFa58j U7GHaaXGNtsY3ah4D2x8Bv9XxPbtN1YoMEV+DFlYAStrIyyUiGXk7yotcBWqbB89/MFt ga9f+5Rhf38/TggWLRD88fBp8u2EIDB5VwqHsX5IstX2+Y0yb+yf4sk9iiToAnIXZhiC Ngqp3YdgLrI6+UlAvixbdB42nlxsGbBJ9WoF4dejLEIeQps47loxNuA2hBsiO1wqSE6n 3XYA=="
},
{
"name": "MIME-Version",
"value": "1.0"
},
{
"name": "X-Received",
"value": "by 10.66.182.162 with SMTP id ef2mr20318738pac.35.1445140881616; Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
},
{
"name": "X-No-Auto-Attachment",
"value": "1"
},
{
"name": "Message-ID",
"value": "<047d7bd6aa64e62f22052259149a#google.com>"
},
{
"name": "Date",
"value": "Sun, 18 Oct 2015 04:01:21 +0000"
},
{
"name": "Subject",
"value": "Infinite Elgintensity just uploaded a video"
},
{
"name": "From",
"value": "YouTube <noreply#youtube.com>"
},
{
"name": "To",
"value": "thulle3-0107#pages.plusgoogle.com"
},
{
"name": "Content-Type",
"value": "multipart/alternative; boundary=047d7bd6aa64e62f000522591497"
}
],
"body": {
"size": 0
},
"parts": [
{
"partId": "0",
"mimeType": "text/plain",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "text/plain; charset=UTF-8; format=flowed; delsp=yes"
}
],
"body": {
"size": 301,
"data": "SW5maW5pdGUgRWx..."
}
},
{
"partId": "1",
"mimeType": "text/html",
"filename": "",
"headers": [
{
"name": "Content-Type",
"value": "text/html; charset=UTF-8"
},
{
"name": "Content-Transfer-Encoding",
"value": "quoted-printable"
}
],
"body": {
"size": 16884,
"data": "DQo8aHRtbCBsYW5nPSJl..."
}
}
]
},
"sizeEstimate": 22314
}
This is how a typical message will look, and all that the various libraries does is to make it easier to work with. The headers will reside inside of the payload, and it's the Subject-header you are looking for.
There is no function for getting a specific header, but it's not that hard to implement:
function getHeader($headers, $name) {
foreach($headers as $header) {
if($header['name'] == $name) {
return $header['value'];
}
}
}
$results = $service->users_messages->listUsersMessages($user);
foreach($results as $mail){
$message = $service->users_messages->get($user, $mail['id']);
$headers = $message->getPayload()->getHeaders();
$subject = getHeader($headers, 'Subject');
}
this works for me
$message = $service->users_messages->get($userId, $messageId,['format' => 'metadata', 'metadataHeaders' => ['From']]);
$headers = $message->getPayload()->getHeaders();
$from = $headers[0]['value']);
if($form == "My From")
....
I was working an mixed 2 or 3 examples and this is the result, in this case i need de Form metadata, change it for Subject and done.
Here is how I read the subject of the emails
$messages = $service->users_messages->listUsersMessages($user, ['q'=> 'from:foo#bar.com ']);
foreach ($messages->getMessages() as $mail)
{
$message = $service->users_messages->get($user, $mail->getId());
$headers = $message->getPayload()->getHeaders();
$subject = array_values(array_filter($headers, function($k){
return $k['name'] == 'Subject';
}));
printf("- %s\n", $subject[0]->getValue());
}

Google Line Chart - v and f properties settings

I am trying to have my line chart to show the result within tooltip as a string format (e.g: Jun 28, 2015 - Jul 4, 2015) while having my hAxis to display its values as date format (e.g: Jun 29).
Similar as Analytics:
However when I draw the chart with the returned Json data shown below:
{
"cols": [{
"id": "A",
"label": "Date Range",
"pattern": "",
"type": "string"
}, {
"id": "B",
"label": "Sessions",
"pattern": "",
"type": "number"
}, {
"id": "C",
"label": "Pageviews",
"pattern": "",
"type": "number"
}],
"rows": [{
"c": [{
"v": "Date(2015,5,23)",
"f": "Jun 23, 2015 - Jun 27, 2015"
}, {
"v": 1645
}, {
"v": 5237
}]
}, {
"c": [{
"v": "Date(2015,5,28)",
"f": "Jun 28, 2015 - Jul 04, 2015"
}, {
"v": 2189
}, {
"v": 6977
}]
}, {
"c": [{
"v": "Date(2015,6,05)",
"f": "Jul 05, 2015 - Jul 11, 2015"
}, {
"v": 2168
}, {
"v": 6862
}]
}, {
"c": [{
"v": "Date(2015,6,12)",
"f": "Jul 12, 2015 - Jul 18, 2015"
}, {
"v": 1661
}, {
"v": 5735
}]
}, {
"c": [{
"v": "Date(2015,6,19)",
"f": "Jul 19, 2015 - Jul 23, 2015"
}, {
"v": 1109
}, {
"v": 3826
}]
}]
}
My chart is showing the hAxis with f property's value instead of v property's value as per shown below:
The data type for the hAxis is set as string.
With the information given, may I ask how I can achieve my desired results?
Have you tried setting the 'options' -> 'vAxis' -> 'ticks' parameter?
Then your json data would look something like this:
{
"cols": [your columns],
"rows": [your rows],
"options": {
"vaxis": {
"ticks": ["jun 29", "jul 6", "jul 13"]
}
}
}
Here's a link to google charts documentation on line charts, I hoe this helps :-)
Edit:
Here's a working jsfiddle. I don't know what method you are using to load your data but this worked for me.
For some reason, the labels were displayed correctly after I set the width and height parameters in options.
Based on the working jsfiddle provided by #bjarkig82. I have figured on how to fix the issue and achieve my objective.
First, the data type for column date needs to be changed from string to date
var cols = [{
"id": "A",
"label": "Date Range",
"pattern": "",
"type": "date"
}, {
"id": "B",
"label": "Sessions",
"pattern": "",
"type": "number"
}, {
"id": "C",
"label": "Pageviews",
"pattern": "",
"type": "number"
}];
var rows = [{
"c": [{
"v": "Date(2015,5,23)",
"f": "Jun 23, 2015 - Jun 27, 2015"
}, {
"v": 1645
}, {
"v": 5237
}]
}, {
"c": [{
"v": "Date(2015,5,28)",
"f": "Jun 28, 2015 - Jul 04, 2015"
}, {
"v": 2189
}, {
"v": 6977
}]
}, {
"c": [{
"v": "Date(2015,6,05)",
"f": "Jul 05, 2015 - Jul 11, 2015"
}, {
"v": 2168
}, {
"v": 6862
}]
}, {
"c": [{
"v": "Date(2015,6,12)",
"f": "Jul 12, 2015 - Jul 18, 2015"
}, {
"v": 1661
}, {
"v": 5735
}]
}, {
"c": [{
"v": "Date(2015,6,19)",
"f": "Jul 19, 2015 - Jul 23, 2015"
}, {
"v": 1109
}, {
"v": 3826
}]
}];
Secondly, I comment the section of code shown below or it will cause my string date (e.g: Jun 28, 2015 - Jul 4, 2015) to show as date (e.g: Jun 28, 2015) within the tooltip instead.
var formatter = new google.visualization.DateFormat({ pattern: "EEEE, MMM dd, yyyy" });
formatter.format(data, 0);
To understand better, please check out this jsfiddle

Categories