its my first Google MyBusiness API encounter.
Im trying to retrieve account locations but ive got nothing.
I dont have direct access for account, my client gave me access to API by json file.
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/plus.business.manage');
$service = new Google_Service_MyBusiness($client);
$accounts = $service->accounts->listAccounts();
and i don't have any error, just Google_Service_MyBusiness_ListAccountsResponse object, then giving foreach on that object and I can get account informations:
object(Google_Service_MyBusiness_Account)[94]
protected 'internal_gapi_mappings' =>
array (size=0)
empty
public 'accountName' => null
public 'accountNumber' => null
public 'name' => string 'accounts/XXXXXXXXXXXXXXXXXXXXX' (length=30)
protected 'organizationInfoType' => string 'Google_Service_MyBusiness_OrganizationInfo' (length=42)
protected 'organizationInfoDataType' => string '' (length=0)
public 'permissionLevel' => null
public 'profilePhotoUrl' => string '.../photo.jpg' (length=94)
public 'role' => null
protected 'stateType' => string 'Google_Service_MyBusiness_AccountState' (length=38)
protected 'stateDataType' => string '' (length=0)
public 'type' => string 'PERSONAL' (length=8)
protected 'modelData' =>
array (size=0)
empty
protected 'processed' =>
array (size=0)
empty
public 'state' =>
object(Google_Service_MyBusiness_AccountState)[84]
protected 'internal_gapi_mappings' =>
array (size=0)
empty
public 'status' => string 'UNVERIFIED' (length=10)
protected 'modelData' =>
array (size=0)
empty
protected 'processed' =>
array (size=0)
empty
Now when i try:
$name = $account->getName(); //accounts/XXXXXXXXXXXXXXXXXXXXX
$service->accounts_locations->listAccountsLocations($name);
i get almost empty Google_Service_MyBusiness_ListLocationsResponse object:
object(Google_Service_MyBusiness_ListLocationsResponse)[88]
protected 'collection_key' => string 'locations' (length=9)
protected 'internal_gapi_mappings' =>
array (size=0)
empty
protected 'locationsType' => string 'Google_Service_MyBusiness_Location' (length=34)
protected 'locationsDataType' => string 'array' (length=5)
public 'nextPageToken' => null
public 'totalSize' => null
protected 'modelData' =>
array (size=0)
empty
protected 'processed' =>
array (size=0)
empty
What is wrong? Am I doing something wrong, or i have wrong credentials(?). How can I check/ask client that he gave me correct access. Can he check Account name in his panel or something?
You appear to be trying to use a service account. As per the doucmentation Basic setup you need to create an Oauth2 client and use that to login to an account that has access to the my business account.
Service accounts do not appear to be supported by Google MyBusiness API as per the documentation.
Related
I have big Object with protected properties and a property can be an array of other Objects. My goal is to print this entire Object as a single nested array. So I need to convert the object to an array.
I've tried doing:
$result = (array) $object;
But this converts only the highest lever object to an array and it messes up my protected properties names with weird question mark signs.
I've also tried something like this but this simply returns an empty array:
$result= json_decode(json_encode($object), true);
Here is what my object looks like:
object(Handling\Model\SearchBooking\Booking)[133]
protected 'jabooknr' => string '018024709' (length=9)
protected 'jitsbooknr' => string '' (length=9)
protected 'status' => string 'Y' (length=1)
protected 'platform' => int 4
protected 'agentid' => string '' (length=6)
protected 'paymentInfo' => null
protected 'transports' =>
array (size=2)
0 =>
object(Handling\Model\SearchBooking\Transport)[145]
protected 'depdate' =>
object(DateTime)[146]
public 'date' => string '2016-12-06 00:00:00.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
protected 'carriercode' => string 'TB' (length=2)
protected 'carriernumber' => string '2067' (length=4)
protected 'brochure' => string '' (length=6)
protected 'pax' =>
array (size=2)
0 =>
object(Handling\Model\SearchBooking\Pax)[147]
protected 'id' => int 1
protected 'title' => string 'MRS' (length=3)
protected 'firstname' => string 'MA' (length=7)
protected 'name' => string 'BEN' (length=5)
protected 'age' => int 58
protected 'luggage' => int 20
protected 'handLuggage' => null
1 =>
object(Handling\Model\SearchBooking\Pax)[148]
protected 'id' => int 2
protected 'title' => string 'MR' (length=2)
protected 'firstname' => string 'P' (length=6)
protected 'name' => string 'FT' (length=4)
protected 'age' => int 60
protected 'luggage' => int 20
protected 'handLuggage' => null
protected 'departureAirport' => string 'BRU' (length=3)
protected 'arrivalAirport' => string 'AGP' (length=3)
1 =>
object(Handling\Model\SearchBooking\Transport)[149]
protected 'depdate' =>
object(DateTime)[150]
public 'date' => string '2016-12-13 00:00:00.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
protected 'carriercode' => string 'TB' (length=2)
protected 'carriernumber' => string '2068' (length=4)
protected 'brochure' => string '' (length=6)
protected 'pax' =>
array (size=2)
0 =>
object(Handling\Model\SearchBooking\Pax)[151]
protected 'id' => int 1
protected 'title' => string 'MRS' (length=3)
protected 'firstname' => string 'MANE' (length=7)
protected 'name' => string 'BN' (length=5)
protected 'age' => int 58
protected 'luggage' => int 20
protected 'handLuggage' => null
1 =>
object(Handling\Model\SearchBooking\Pax)[152]
protected 'id' => int 2
protected 'title' => string 'MR' (length=2)
protected 'firstname' => string 'PIRE' (length=6)
protected 'name' => string 'FYT' (length=4)
protected 'age' => int 60
protected 'luggage' => int 20
protected 'handLuggage' => null
protected 'departureAirport' => string 'AGP' (length=3)
protected 'arrivalAirport' => string 'BRU' (length=3)
protected 'extraLuggage' => null
EDIT
I have a method in my class where I "find" the result that looks like this:
public function findBooking()
{
//here happens a bunch of logic to get the right result
var_dump($object); exit; // this is the result that is show above
return $object;
}
There are a few issues, that make this difficult.
Property visibility, (private, protected) can cause issues when trying to read them outside of the class, proper. This is expected behavior as that's the point to not use public.
Classes are different. They are well defined and we know them ahead of time, but they are too diverse to account of all property names, at least not with a lot of wasted effort. Not to mention defining them "hard coding" would bite you later as it would make it difficult to maintain. For example if one of the packages does an update and you have coded the property names in you may have issues if they change them. On top of this given that these properties are not part of the classes Public "API" but instead part of the internals, it would not be unreasonable for them to change.
Properties can contain a mix of data types, including other classes or objects. This can make it challenging to handle.
Classes are part of other packages/frameworks and editing them is not practical, this restricts us to working outside of these classes.
So given these difficulties I would recommend using reflection to access the protected properties. Reflection allows you to inspect the definition of classes (and other stuff).
function jsonSerialize($obj){
return json_encode(toArray($obj));
}
function toArray($obj){
$R = new ReflectionObject($obj);
$proerties = $R->getProperties();
$data = [];
foreach($proerties as $k => $v){
$v->setAccessible(true);
$property = $v->getName();
$value = $v->getValue($obj);
if(!is_object($value)){
$data[$property] = $value;
}else if( is_a($obj,'\\DateTime')){
//if its a descendant of Datetime, get a formatted date.
// you can add other special case classes in this way
$data[$property] = $value->format('Y-m-d H:i:s');
}else{
$data[$property] = toArray($value); //call recursively
}
}
return $data;
}
So assume we have these classes
class foo{
private $bar; //private nested object
public function __construct(){
$this->bar = new bar();
}
}
class bar{
private $something = 'hello';
}
$obj = new foo;
echo jsonSerialize($obj);
See it in a sandbox here
Outputs:
{"bar":{"something":"hello"}}
Also of note is we have a special consideration for the DateTime class. Instead of getting all the properties of this we just want the date (probably) formatted in some standard way. So by using is_a() (I'm old school) we can tell if the Object $value has a given class as an ancestor of it. Then we just do our formatting.
There are probably a few special cases like this, so I wanted to mention how to handle them.
Though it is an old query, most answers are not easy to follow. So I tried to simplify the code for this specific question.
The cleaner way to get JSON objects is by implementing the JsonSerializable interface.
class Booking implements JsonSerializable
{
protected $jabooknr;
protected $platform;
//Other attributes ....
//Array of tronsport
protected $transports;
protected $extraLuggage;
public function jsonSerialize()
{
return [
'jabooknr'=> $this->jabooknr,
'platform'=> $this->platform,
'transports' => [json_encode($this->transports)
],
'$extraLuggage' => $this->extraLuggage
];
}
public function __construct($jabooknr, $platform){
$this->jabooknr = $jabooknr;
$this->platform = $platform;
$this->transports=[new Transport()];
}
}
class Transport implements JsonSerializable{
protected $carriercode;
protected $carriernumber;
//Array of Pax
protected $pax ;
public function jsonSerialize()
{
return [
'carriercode'=> $this->carriercode,
'carriernumber'=> $this->carriernumber
];
}
}
$booking = new Booking('018024709',25);
echo json_encode($booking);
so i am using an API whose return is protected members.
My code :-
var_dump($a)
Gets me something like this :-
array (size=1)
0 =>
object(Klarna\XMLRPC\Address)[26]
protected 'email' => string '' (length=0)
protected 'telno' => string '' (length=0)
protected 'cellno' => string '' (length=0)
protected 'fname' => string 'Testperson-se' (length=13)
protected 'lname' => string 'Approved' (length=8)
How can I access the protected members?
I tried to do it via :- var_dump($addrs->country) but it doesn't work.
You cannot access a protected method/property outside of that class. That's why is called "protected". You have to create a public getter:
public function getEmail()
{
return $this->email;
}
and use it as $a->getEmail(); and so on for rest of properties you need
I have a problem with accessing one of the properties in stdClass returned from Twitter api, I'm trying to get property named extended_entities however PHP constantly returns error complaining that the property doesn't exist. I can clearly see it thoug in var_dump. See output:
object(stdClass)[597]
public 'created_at' => string 'Mon Mar 23 16:39:25 +0000 2015' (length=30)
public 'id' => int 580046201061580800
public 'id_str' => string '580046201061580800' (length=18)
...
public 'user' =>
object(stdClass)[600]
...
...
public 'notifications' => boolean false
public 'geo' => null
...
public 'entities' =>
object(stdClass)[581]
public 'hashtags' =>
array (size=0)
empty
public 'symbols' =>
array (size=0)
empty
public 'user_mentions' =>
array (size=0)
empty
public 'urls' =>
array (size=0)
empty
public 'media' =>
array (size=1)
0 =>
object(stdClass)[593]
...
public 'extended_entities' =>
object(stdClass)[573]
public 'media' =>
array (size=2)
0 =>
object(stdClass)[556]
...
1 =>
object(stdClass)[595]
...
public 'favorited' => boolean false
public 'retweeted' => boolean false
public 'possibly_sensitive' => boolean false
public 'possibly_sensitive_appealable' => boolean false
public 'lang' => string 'en' (length=2)
I have no problem getting property for example retweeted, created_at etc. in the following way:
$response->retweeted //this returns false
$response->user //returns stdClass
however
$response->extended_entities
or
$response->extended_entities->media
returns
Notice: Undefined property: stdClass::$extended_entities
I expect to have object or array respectively. I don't understand why I can access some of the properties and this one not?
I am trying to access the $this->agent object in CodeIgniter 2, but all values (variables) are NULLed since the model where I'm accessing $this->agent is passed through the CURL request.
I have a test controller OUTBOUND from which the CURL request is made to another controller named INBOUND for example.
Once the schema is validated and all checkings are passed with success, the INBOUND calls the following function inbound():
public function inbound()
{
// Load dependencies
$this->load->library('user_agent');
var_dump($this->agent);
die;
}
and it returns me the following dump:
object(CI_User_agent)[28]
public 'agent' => null
public 'is_browser' => boolean false
public 'is_robot' => boolean false
public 'is_mobile' => boolean false
public 'languages' =>
array (size=0)
empty
public 'charsets' =>
array (size=0)
empty
public 'platforms' =>
array (size=0)
empty
public 'browsers' =>
array (size=0)
empty
public 'mobiles' =>
array (size=0)
empty
public 'robots' =>
array (size=0)
empty
public 'platform' => string '' (length=0)
public 'browser' => string '' (length=0)
public 'version' => string '' (length=0)
public 'mobile' => string '' (length=0)
public 'robot' => string '' (length=0)
Is there any proper way to not loose these information after the CURL request is made, I'm mean in INBOUND controller?
**** UPDATED ****
Is it possible to solve this problem without need to tell the 3rd party to include these informations into their CURL request ?
This is a really simple task, that has me absolutely stumped! I'm pulling back some JSON via CURL and PHP, and attempting to access the data from the below structure:
object(stdClass)[1]
public 'maxResults' => int 43
public 'resultList' =>
array (size=43)
0 =>
object(stdClass)[2]
public '#class' => string '' (length=64)
public 'id' => int
public 'version' => int 0
public 'dateCreated' => string '2014-02-11T18:37:55.835+0000' (length=28)
public 'dateModified' => null
public 'locationId' => int
public 'departmentId' => int
public 'ownerCompanyId' => int
public 'active' => boolean true
public 'userId' => int
public 'userName' => string '' (length=24)
public 'externalCode' => null
public 'employeeDetails' =>
object(stdClass)[3]
...
public 'chargeBandAllocationsIds' =>
array (size=7)
...
public 'personalRateChargeBandId' =>
object(stdClass)[13]
...
public 'employeeGroupIds' =>
array (size=0)
...
public 'isResource' => boolean false
(I've removed some of the values, for privacy reasons)
Now I'm trying to var_dump using var_dump(json_decode($result, false));, however when I try and get into the 'resultList' array using var_dump(json_decode($result['resultList'], false)); I get an illegal string offset error.
$result is the JSON string, your cannot do $result['resultList'] on a string. It only becomes a structure after you json_decode it. However, you're decoding it as object, not array, so this wouldn't work either way.
$data = json_decode($result);
var_dump($data->resultList);
var_dump($data->resultList[0]);
var_dump($data->resultList[0]->id);
foreach ($data->resultList as $employee) {
var_dump($employee->id);
}