Getting documents from MongoDB as PHP objects - php

I am developing a PHP application using the Twitter API.I have already achieved the data store from REST API(format JSON) in a MongoDB which can later be accessed by my web application.I want to return a document from mongodb is a PHP object, not an array.Because, the PHP Driver return always MongoDB documents as arrays.I dont know how.

For an example MongoDB collection of documents:
{
_id : "my_unique_id_1",
myfield1 : 100,
myfield2 : "myvalue"
}
you should be able to perform action like following:
$myobject = (object)$mycollection->findOne(array("_id" => "my_unique_id_1"));
echo($myobject->myfield1);
In general, there is always a possibility to convert PHP array to php object like below:
$php_array = array("key1"=>"value1", "key2"=>"value2");
$php_object = (object)$php_array;
echo($php_object->key1); // should give "value1"
Here you can also find some information on casting between object and array types in PHP:
PHP - Types - Object casting
PHP - Type juggling

Related

php create several objects within object

I am trying to make a soap request and it needs to have this structure
$requestBody=new sendDataExtraccionRequest(new sendDataExtraccionSubterranea("18-09-2020","00:02:01",1234567891,12345678.12,12345678.12),
new sendDataExtraccionSubterranea("18-09-2020","00:03:01",1234567891,12345678.12,12345678.12));
thought that by creating an array with each object and then cast it should do, but getting an error on the soap call that the date field is missing
$array_datos[] = new sendDataExtraccionSubterranea("03-02-2021","00:02:01",1234567891,12345678.12,12345678.12);
$array_datos[] = new sendDataExtraccionSubterranea("03-02-2021","00:03:01",1234567891,12345678.12,12345678.12);
$requestBody=new sendDataExtraccionRequest( (object)$array_datos );
Also tried a solution that involved json encoding and decoding the array, but same error
Any hint on how to achieve it?
Thanks
Use Spread Operator like this
$requestBody=new sendDataExtraccionRequest(...$array_datos);

How To Use 'hasFile & 'file' or Equivalents When Utilizing '->only' on Request Object in Laravel

For fun I am trying to figure out different ways of doing things in PHP & Laravel (the version of Laravel I am using is 5.8.37) and I am having trouble when I use the 'only' arrow function (->only) on a request object variable.
For example, if I do the following to store only certain fields from my request like so:
$imageRequest = ($request->only('image_file', 'filename'));
This ends up working out well to get the two inputs/data I require, but the result is stored in an array, which is where the problem I am trying to understand how to overcome arises.
After storing just the values I need, I would like to pass $imageRequest to a function, and in that function I would like to check if $imageRequest->hasFile, as well as accessing the file from it like so:
if($imageRequest->hasFile('image_file')){
...
$image = $imageRequest->file('image_file');
...
}
I have tried to convert the results of the ->only array to an object but I am still unable to utilize ->hasFile or ->file. I know that these arrow functions are meant to be used with request objects.
Is it possible to achieve what I am trying to do with ->only (limiting what inputs/data I choose to include) but maintaining the functionality of ->hasFile and ->file?

Instantiate an stdClass object from json schema specification in PHP

I have a set of JSON requests that I must send to a RESTFul API in order to get some response objects, you know, the usual thing for a webapp, however these API request objects are properly documented with a json schema specification for each, so I would like to load those schema files and create stdClass object instances based on that info automagically.
Is there some way to do this with a library or something in PHP? (don't want to reinvent the wheel)
Thanks!
Edit: Have a look at this schema file which contains an example of what I want to load and build object instances from.
Disclaimer: I do know json_encode / json_decode which is not what I'm looking for. Using that I'd need to traverse through the returned schema object and then create another object/array based on the schema read, which is not what I want.
I don't think there's a built-in way of doing this, but it should be relatively trivial to implement:
function createObj( $json ) {
$obj_schema = json_decode($json, true);
$new_obj = new StdClass;
foreach($obj_schema['properties'] as $property) {
$new_obj->{$property} = null;
}
return $new_obj;
}

Simplest way to communicate data between php and android

I am trying to build an android application based on hotel management. The app simply provides the list of deals to the user. The website stores the data regarding the user and deals in sql tables in database.
Is there any simple way to communicate data such as deal details and user details like user name, hotel name, etc. from website to android app and viceversa.
I know this is possible using json. But I am finding it difficult to implement.
it is very painless using https://code.google.com/p/google-gson/ on android and json_encode() in PHP (take also a look on blackpanthers response) and here a short excerpt how to bind that thing in java on a concrete sample:
first implement json_encode() in PHP and call in in a browser so you can see the result, say, you get something like:
{"user_name":"my name", "user_surname":"my_surname", "phones": { "mobile":"11111" }}
now, you've got a JSON entity containing a properties "user_name", "user_surname", and "phones". whereas "phones" is a nested entity, containing a property "mobile".
now you create a java-class per entity, so we need two, the one containing the "phones" and the other one containing all properties, including the entity "phones"
class Phones {
// with this annotation you can bind to
// properties from JSON named differently than
// the property in this class
#SerializedName("mobile")
String thePhone;
}
class MyJson {
String user_name;
String user_surname;
Phones phones;
}
well, thats it :) ah ok, the final part
...
InputStream is = new URL("http://www.my.php.returning.json").openStream();
InputStreamReader isr = new InputStreamReader();
MyJson myJson = new Gson().fromJson(isr , MyJson.class);
... //close stream, handle exceptions, etc.
// now you've got that all in the myJson object...
here you go!
You may find the following tutorial useful: http://www.helloandroid.com/tutorials/connecting-mysql-database
This tutorial shows how you can use the MySQL database and PHP to send JSON to an android application.
(This is assuming you are using a MySQL database, if not, it is still a useful tutorial).
You can also use GSON, Google's API and the following tutorial:
http://www.techrepublic.com/blog/app-builder/use-gson-to-work-with-json-in-your-android-apps/1386

PHP Web Service - Same call: Multiple records = array, 1 record=single object - How to work around?

I'm consuming a web service in PHP. If the service returns 2 or more records the object comes back as an array. However, if I call the same service that returns 1 record, the object is not an array. This makes for some messy logic having to watch for both cases when one would think PHP could be smart enough to handle this appropriately and always return an array of 1 element.
So my question is - is there a way to force the return object to always be an array? Some property in the call or something?
EDIT
I'm using PHP's soapclient library. The service is an in-house one that returns an array of a custom class.
you could try the following:
$client = new SoapClient("http://host/services/some.wsdl",
array('feature' => SOAP_SINGLE_ELEMENT_ARRAYS));
This should make php behave the way you want.
Also you might find this dotvoid article interesting.
HTH

Categories