I m getting following response from a third party api using php. I want to retrieve "data" named key value. In simple words, I want to retrieve value of "#data". I have no idea why # sign is used with "data" named key.
array:1[
dataInfo: DataPartInfo {
#data: b"""
}
]
Related
I am trying to retrieve some part of request() in my Form Request class named StoreApplicantLanguage.php. The request key called 'languages' and it has an array of objects containing a key-value pair to be stored in my `applicant_languages' table.
Here is my JSON request from Postman:
{
"languages": [
{
"language": "English",
"capability": 1
}
]
}
Looks normal right?! But, when I'm trying to get the values of the languages key like this:
$requestLanguages = request()->languages;
dd($requestLanguages);
, it shows null.
I tried to restart my server, do php artisan config:cache, but none are works. But when I change the key name in the request object to language, it works!
Also, the request object has another named field like families, and I can get the values inside by doing request()->families.
I have no idea at all how this can be happen. Anyone can explain my case, please!
Thanks in advance!
Edit: From Malkhazi Dartsmelidze's answer I realized that I misstyped the question. I didn't write comma after '1' value in my JSON request
It works fine on my system.
Maybe you that's because you are passing invalid json.
{
"languages": [
{
"language": "English",
"capability": 1
}
]
}
Try passing this JSON (I deleted last comma after '1')
Also note that Request is object and there is properties that are used already and $request variable can return it. You can use $request->get('languages') to get parameter from request
I would like to filter some data coming from an API payload in which i have to check if some certain part of the data is an object, such as this:
"object"{
"propety":value,
"another_propety":value,
}
I wanna be sure that the "object" that comes from the payload is actually an object, which holds properties and not an integer, neither, an array or any other type...but an object. Is there any way i can solve this by using Laravel's native validator i have to create a custom rule?
Thank you
Considering the laravel lifecycle, By the time the request payload reaches validation the object has already changed to a php array, use the below to validate your key.
$this->validate($request, [
'object' => 'required|array',
'object.property' => 'required|string',
]);
https://laravel.com/docs/5.8/validation#rule-array
also in case it is somehow going to remain a JSON object, check the official documentation for doing so -> https://laravel.com/docs/5.8/validation#rule-json
This will help you identify the object key as JSON while the request gets vaildated.
I'm using php sdk to delete some fields from firestore database,
i want to delete a map from an array inside a document, but instead the function used delete all maps inside the parent array.
My firestore database looks like this
What i'm trying to do is remove a specific index ex: index 0 with it's children from imageUrls array not all the maps inside it.
I tried these 2 functions :
$usersRef->update([
['path' => 'imageUrls.image_url', 'value' => FieldValue::arrayRemove(['image.png'])]
]);
and this one
$usersRef->update([
['path' => 'imageUrls.image_url', 'value' => FieldValue::deleteField()]
]);
The first function remove all imageUrls childrens and change imageUrls type from array to map, while the second one nothing happened. All the fields still exist in the document and no deletion occurd
<?php
namespace App\Services;
use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Firestore\FieldValue;
use Google\Cloud\Firestore\FieldPath;
class FirebaseService
{
public function delete()
{
// Create the Cloud Firestore client
$db = new FirestoreClient(
['projectId' => 'MyProjectId']
);
$usersRef = $db->collection('NewStories')->document('1');
$usersRef->update(
[
['path' =>'imageUrls.image_url',
'value' => FieldValue::arrayRemove(['image.png'])]
]);
}
}
This can be achieved using the arrayRemove() method. As the PHP Client for Firestore says
Returns a special value that can be used with set(), create() or update()
that tells the server to remove the given elements from any array value
that already exists on the server. All instances of each element
specified will be removed from the array. If the field being modified is
not already an array it will be overwritten with an empty array.
Update:
Firebase does not support updating an existing element in an indexed array.
More information can be found in the Official Documentation.
Workaround:
Reading the entire array out of the document, make modifications to it in memory, then update the modified array field entirely.**
Credits to this Firestore Update single item in an array field case.
I'm currently working on building an API with the Symfony framwork. I've done enough reading to know to use the Serialization component, and built some custom normalizers for my entities. The way it currently works is:
JSON -> Array(Decode) -> User Entity(Denormalize)
This was working find as long as the request content was a JSON representation of the user, example:
{
"email": "demouser#email.com",
"plainPassword": "demouser",
"first_name" : "Demo",
"last_name" : "User"
}
A user entity is created using the following code in my controller:
$newuser = $this->get('api.serializer.default')->deserialize($request->getContent(), WebsiteUser::class, 'json');
However, I'd like to nest the user JSON in the 'data' property of a JSON object, which will allow consumers to pass additional metadata with the request, example:
{
"options": [
{
"foo": "bar"
}
],
"data": [
{
"email": "demouser#email.com",
"plainPassword": "demouser",
"first_name": "Demo",
"last_name": "User"
}
]
}
The main issue this causes is that the deserialization does not succeed because the JSON format has changed.
The only solution I've considered so far is to json_decode the whole request body, grab the 'data' element of that array, and pass the contents of the data element to the denormalizer (instead of the deserializer).
Is there a better way to solve this problem?
You should be able to get a specific key of your request body like follows:
$newuser = $this->get('api.serializer.default')->deserialize(
$request->request->get('data'), WebsiteUser::class, 'json'
);
If you are not able to retrieve the data from key without decoding your request body, look at this bundle, it consists in only one EventListener that replaces the request body after decode it.
You can easily integrate the same logic in your application, or requiring the bundle directly (which works well).
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