PHP undefined property in Twitter response - php

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?

Related

Google MyBusiness API PHP Locations

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.

Save multiple associated entities in one go Cakephp

i've an Order that saves, but the multiple orderlines (a product and total price, quantity) for each order isnt saving together with the order.
Here is the log dump at this stage:
{ "associated": [ "Orderlines", "Payments" ], "Orderlines": [ { "total_cost": "222.44", "total_quantity": "2", "product_variants_id": "34" }, { "total_cost": "154", "total_quantity": "2", "product_variants_id": "33" } ], "users_id": 1, "usersaddress_id": 1, "orderstatus": 0, "date": "2017-09-21T01:53:38+00:00", "last_modified": "2017-09-21T01:53:38+00:00" }
Controller:
$associated = ['Orderlines', 'Payments'];
$order = $this->Orders->newEntity(['associated'=>$associated]);
if ($this->request->is('post')) {
$order = $this->Orders->patchEntity($order, $this->request->getData());
$order->users_id = $this->Auth->user('id');
//Hardcoded the SHU MART stores location because thats all thats gonna get implemented for now
$order->usersaddress_id = 1;
$order->orderstatus = 0;
$order->date = Time::now();
$order->last_modified = Time::now();
//god this is ugly. forgive me for i have sinned
$order->setDirty('users_id', true);
$order->setDirty('usersaddress_id', true);
$order->setDirty('date', true);
$order->setDirty('last_modified', true);
$order->setDirty('orderlines', true);
if ($this->Orders->save($order)) {
$this->Flash->success(__('The order has been saved.'));
return $this->redirect(['controller'=>'orderlines','action' => 'index']);
}
$this->Flash->error(__('The order could not be saved. Please, try again.'));
}
and the form inside the orderline loop:
<?php
echo $this->Form->control('Orderlines.'.$orderLineIndex.'.total_cost',
['label'=>'', 'value'=>$product_total
, 'type'=>'hidden'
]);
?>
<?php
echo $this->Form->control('Orderlines.'.$orderLineIndex.'.total_quantity',
['label'=>'', 'value'=>$item['quantity']
, 'type'=>'hidden'
]);
?>
<?php
echo $this->Form->control('Orderlines.'.$orderLineIndex.'.product_variants_id',
['label'=>'', 'value'=>$item['id']
, 'type'=>'hidden'
]);
?>
Now i've tried it without Orderlines. at the start, and tried saving each orderline individually but it wont save the order id and stuff.
I just want to be able to save it in one go. Something like:
order{ id, x, y, orderlines[[0]{OL1...}[1]{OL2}] }
So i can have as many orderlines in an order and save them all together each time. I think im just missing a small syntax thing, but maybe im not.
public 'associated' =>
array (size=2)
0 => string 'Orderlines' (length=10)
1 => string 'Payments' (length=8)
public 'orderlines' =>
array (size=3)
0 =>
object(App\Model\Entity\Orderline)[349]
public 'total_cost' => float 40.28
public 'total_quantity' => int 1
public 'product_variants_id' => int 679
public '[new]' => boolean true
public '[accessible]' =>
array (size=2)
...
public '[dirty]' =>
array (size=3)
...
public '[original]' =>
array (size=0)
...
public '[virtual]' =>
array (size=0)
...
public '[errors]' =>
array (size=0)
...
public '[invalid]' =>
array (size=0)
...
public '[repository]' => string 'Orderlines' (length=10)
1 =>
object(App\Model\Entity\Orderline)[375]
public 'total_cost' => float 66
public 'total_quantity' => int 2
public 'product_variants_id' => int 55
public '[new]' => boolean true
public '[accessible]' =>
array (size=2)
...
public '[dirty]' =>
array (size=3)
...
public '[original]' =>
array (size=0)
...
public '[virtual]' =>
array (size=0)
...
public '[errors]' =>
array (size=0)
...
public '[invalid]' =>
array (size=0)
...
public '[repository]' => string 'Orderlines' (length=10)
2 =>
object(App\Model\Entity\Orderline)[347]
public 'total_cost' => float 222.44
public 'total_quantity' => int 2
public 'product_variants_id' => int 34
public '[new]' => boolean true
public '[accessible]' =>
array (size=2)
...
public '[dirty]' =>
array (size=3)
...
public '[original]' =>
array (size=0)
...
public '[virtual]' =>
array (size=0)
...
public '[errors]' =>
array (size=0)
...
public '[invalid]' =>
array (size=0)
...
public '[repository]' => string 'Orderlines' (length=10)
public 'users_id' => int 1
public 'usersaddress_id' => int 1
public 'orderstatus' => int 0
public 'date' =>
object(Cake\I18n\Time)[327]
public 'time' => string '2017-09-27T21:12:38+10:00' (length=25)
public 'timezone' => string 'Australia/Melbourne' (length=19)
public 'fixedNowTime' => boolean false
public 'last_modified' =>
object(Cake\I18n\Time)[350]
public 'time' => string '2017-09-27T21:12:38+10:00' (length=25)
public 'timezone' => string 'Australia/Melbourne' (length=19)
public 'fixedNowTime' => boolean false
public '[new]' => boolean true
public '[accessible]' =>
array (size=3)
'*' => boolean true
'id' => boolean false
'users_id' => boolean false
public '[dirty]' =>
array (size=7)
'associated' => boolean true
'orderlines' => boolean true
'users_id' => boolean true
'usersaddress_id' => boolean true
'orderstatus' => boolean true
'date' => boolean true
'last_modified' => boolean true
public '[original]' =>
array (size=1)
'orderlines' =>
array (size=3)
0 =>
object(App\Model\Entity\Orderline)[359]
...
1 =>
object(App\Model\Entity\Orderline)[372]
...
2 =>
object(App\Model\Entity\Orderline)[346]
...
public '[virtual]' =>
array (size=0)
empty
public '[errors]' =>
array (size=0)
empty
public '[invalid]' =>
array (size=0)
empty
public '[repository]' => string 'Orders' (length=6)
Thanks.
So there are 2 answers i got. The first being the really-messy-but-works solution, and the one i found later.
if ($result) {
$orderlinescontroller = new OrderlinesController();
if ($orderlinescontroller->addAllFromCart($result->id)) {
$this->Flash->success(__('The order ' . $result->id . ' has been successfully placed'));
$this->clearUsersCart($this->Auth->user('id'));
so first you save the order, then create an instance of the controller of the associated item you need to save multiple of. Then i created a function that looks at the db/session variables for the cart items to save them with the order.
public function addAllFromCart($order_id)
{
$success = true;
$cart_items = $this->getCartItemsArray();
$saved_ids = [];
foreach ($cart_items as $cart) {
$this->log($cart);
$orderline = $this->makeEntity($order_id, $cart);
$new = $this->Orderlines->save($orderline);
if ($new) {
$this->log('Line saved');
array_push($saved_ids, $new->id);
} else {
$this->log('Line save FAILED');
$success = false;
//if there is a failure, we need to delete the lines that we made - there is a better way, but cake sucks and conventions don't work
foreach ($saved_ids as $id_to_delete) {
$d = $this->Orderlines->get($id_to_delete);
$this->Orderlines->delete($d);
}
}
}
$this->log('WAS ADD ALL FROM CART A SUCCESS? -- ' . $success);
// if ($success) {
// return $this->redirect($this->referer());
// } else {
// return $this->redirect($this->referer());
// }
return $success;
}
`
which altogether is super messy and un-cake-ey but it works. The other solution i found out much later when i revisited a part of adding products, was how it explicitly told save() what associations to save. Hence the below was used.
$result = $this->Orders->save($save, ['associated' => ['Orderlines']]);

JMS serializer - Why are new objects not being instantiated through constructor

Why are new entities instantiated with null for all values except the data in the json, why is the entity constructor not setting defaults - putting a die() in the constructor never gets executed.
Update:
Ok so digging into the code, when no managed entity is found, JMSS will use the doctrine instantiator class to create the entity - its sole job, to create entities without calling the constructor. Is there a reason for this? this is inside JMS\Serializer\Construction\UnserializeObjectConstructor
I've configured the object constructor to use the doctrine object constructor written by JMS, but the same issue happens with and without this.
jms_serializer.object_constructor:
alias: jms_serializer.doctrine_object_constructor
public: false
Existing entities are updated without trouble, however new entities are missing all constructor set defaults.
Under 'fields' element 0 is existing, element 1 is new.
array (size=3)
'id' => int 2
'name' => string 'Categories' (length=10)
'fields' =>
array (size=2)
0 =>
array (size=7)
'id' => int 49
'displayName' => string 'Car Branded' (length=11)
'type' => string 'checkboxlist' (length=12)
'required' => boolean false
'disabled' => boolean false
'name' => string 'h49' (length=3)
1 =>
array (size=3)
'type' => string 'email' (length=5)
'name' => string 'field3491' (length=9)
'displayName' => string 'Email' (length=5)
The entity looks like this after deserializing:
object(stdClass)[2000]
public '__CLASS__' => string 'AppBundle\Entity\FormElement' (length=28)
public 'id' => null
public 'label' => string 'Email' (length=5)
public 'type' => string 'email' (length=5)
public 'defaultValue' => null
public 'required' => null
public 'mappedField' => null
public 'garbageCollection' => null
public 'sortOrder' => null
public 'disabled' => null
public 'uuid' => null
public 'form' => null
public 'multiOptions' => null
public 'filters' => null
public 'submissions' => null
The entity constructor:
public function __construct()
{
$this->required = false;
$this->disabled = false;
$this->garbageCollection = false;
$this->sortOrder = 0;
$this->type = 'text';
}
And finally this is how im deserializing:
$serializer = $this->get('jms_serializer');
$entryForm = $serializer->deserialize($json_data, 'AppBundle\Entity\EntryForm', 'json');
The issue is the default ObjectConstructor uses Doctrine's Instantiator, which does not call the class' constructor. To solve this, you can create your own ObjectConstructor that just returns a new instance of the class.
Example:
<?php
namespace AppBundle\Serializer;
use JMS\Serializer\Construction\ObjectConstructorInterface;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\VisitorInterface;
class ObjectConstructor implements ObjectConstructorInterface
{
/**
* {#inheritdoc}
*/
public function construct(
VisitorInterface $visitor,
ClassMetadata $metadata,
$data,
array $type,
DeserializationContext $context
) {
$className = $metadata->name;
return new $className();
}
}
If you're using the bundle, just set jms_serializer.unserialize_object_constructor.class parameter to that new class. Otherwise in your builder, use the class as your object constructor.
What worked for me was simply adding this to the jms_serializer config:
jms_serializer:
object_constructors:
doctrine:
fallback_strategy: "fallback"

How to retrieve user_agent data after CURL request in CodeIgniter 2?

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 ?

Drilling down into JSON with PHP

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);
}

Categories