How can I update multiple items on firebase with just one request?
I can not let my system wait to complete multiple requests, so I need to update them with just one.
Current Firebase Data:
PHP Code:
$data = [
'mydata-1' => [
'position' => 1,
],
'mydata-2' => [
'position' => 2,
],
];
$client = new \GuzzleHttp\Client();
$response = $client->put(FIREBASE_ENDPOINT . '/data', [
'json' => $data,
]);
Expected Firebase Data:
Problem:
The request subscribe all my data and I lose the title field.
I also tried PATCH request, but I got the same response.
Based on Frank van Puffelen comment:
$data = [
'mydata-1/position' => 1,
'mydata-2/position' => 2,
];
$client = new \GuzzleHttp\Client();
$response = $client->patch(FIREBASE_ENDPOINT . '/data', [
'json' => $data,
]);
Documentation:
https://firebase.googleblog.com/2015/09/introducing-multi-location-updates-and_86.html
Related
I'm working on Lavravel 9 with guzzle to send some data in
the below code need to get some data from the database how can get data from the database?.
$response = Http::post('APIurl', [
"headers" => [
//header information
],
"body" => [
'title' => "**Get data from database**",
'body' => "**Get data from database**,
'userId' => 22
],
]);
Thank you
Assuming your API returns a json result, simply call the json() method on the Response object. If you want the raw response body, use body() instead.
For example, take github's api:
$response = Http::get('api.github.com'); // replace with your call
$body = $response->body(); // returns string
$json = $response->json(); // tries to return a json array.
To add data to your request, try passing a json string in the body.
$data = json_encode([
'title' => ...,
'body' => ...,
'user_id' => ...,
]);
$response = Http::post('your-api-here', [
'headers' => [
...
],
'body' => $data,
]);
I am trying to hide all gridlines from a file I just created with Google Sheets API, I am using the following code:
$service = $this->GoogleServiceSheets();
$sheetID = null;
$worksheetSheets = $service->spreadsheets->get($fileID)->sheets;
foreach($worksheetSheets as $sheet){
$sheetID = $sheet->properties['sheetId'];
break;
}
$service = $this->GoogleServiceSheets();
$requests = [
new \Google_Service_Sheets_Request([
'updateDimensionProperties' => [
'range'=> new \Google_Service_Sheets_DimensionRange([
'sheetId' => $sheetID,
'dimension' => 'COLUMNS',
'startIndex' => 0,
'endIndex' => 1000
]),
'properties'=> new Google_Service_Sheets_DimensionProperties([
"hideGridlines"=> True,
]),
'fields' => 'hideGridlines'
]
])
];
$batchUpdateRequest = new \Google_Service_Sheets_BatchUpdateSpreadsheetRequest(['requests' => $requests]);
$service->spreadsheets->batchUpdate($fileID, $batchUpdateRequest);
But does not hide, instead gives me an error.
What I am doing wrong?
In order to hide the grid line of the Google Spreadsheet with Sheets API, UpdateSheetPropertiesRequest of batchUpdate is used. Unfortunately, updateDimensionProperties cannot achieve this. I think that this is the reason of your issue. In order to achieve your goal, when your script is modified, it becomes as follows.
Modified script:
In this modification, your $requests is modified as follows.
$requests = [
new \Google_Service_Sheets_Request([
'updateSheetProperties' => [
'properties' => [
'sheetId' => $sheetID,
'gridProperties' => [
'hideGridlines' => true
],
],
'fields' => 'gridProperties.hideGridlines'
],
])
];
Note:
In this modification, it supposes that your $service = $this->GoogleServiceSheets(); and $sheetID are valid values. And also, it supposes that you have already been able to get and put values for Google Spreadsheet using Sheets API. Please be careful this.
Reference:
UpdateSheetPropertiesRequest
I have an API and I need to send some data to it and I am using guzzle for handling it so here is my code:
$amount = $request->get('amount');
$client = new \GuzzleHttp\Client();
$requestapi = $client->post('http://192.168.150.16:7585/api/v1/Transaction/GetTransactionNumber', [
'headers' => ['Content-Type' => 'application/json'],
'body' => '{
"Amount":"i want to send $amount here",
"something":"1",
"Description":"desc",
}'
]);
so every thing is fine and static data is being send but I want to know how can I send a variable.
You can bind the data in form_params parameter like
$client = new \GuzzleHttp\Client();
$amount = $request->get('amount');
$requestapi = $client->post('http://192.168.150.16:7585/api/v1/Transaction/GetTransactionNumber', [
'form_params' => [
"Amount" => "i want to send $amount here",
"something" => "1",
"Description" => "desc",
]
]);
Hope this works for you.
Amount can pass in an array and after you can encode with json using ```json_encode``
Hope this works for you.
$amount = $request->get('amount');
$client = new \GuzzleHttp\Client();
$url = "http://192.168.150.16:7585/api/v1/Transaction/GetTransactionNumber";
$data = [
"amount" => $amount,
"something" => "1",
"description" => "desc",
];
$requestAPI = $client->post( $url, [
'headers' => ['Content-Type' => 'application/json'],
'body' => json_encode($data);
]);
You can try to use Guzzle json option:
$amount = $request->get('amount');
$client = new \GuzzleHttp\Client();
$response = $client->post(
'http://192.168.150.16:7585/api/v1/Transaction/GetTransactionNumber',
[
GuzzleHttp\RequestOptions::JSON => [
'Amount' => $amount,
'something' => '1',
'Description' => 'desc',
]
]
);
Check Guzzle manual - http://docs.guzzlephp.org/en/stable/request-options.html#json
I've managed to go through creating webhook using Shopify API, but I can create only one webhook per request. I've already tried to customize the request so it could possibly create a few webhooks at once, but it doesn't seem to work.
I'm using GuzzleHttp\Client for my requests and this is what my working request look like:
$client = new Client();
$response = $client->request(
'POST',
"https://{$store}/admin/webhooks.json",
[
'headers' => [
'X-Shopify-Access-Token' => $access_token,
'X-Shopify-Shop-Domain' => $store
],
'form_params' => [
'webhook' => [
"topic" => "orders/create",
"address" => $appAddress,
"format" => "json"
],
]
]);
But when I try something like this:
$client = new Client();
$response = $client->request(
'POST',
"https://{$store}/admin/webhooks.json",
[
'headers' => [
'X-Shopify-Access-Token' => $access_token,
'X-Shopify-Shop-Domain' => $store
],
'form_params' => [
'webhook' => [
[
"topic" => "orders/create",
"address" => $appAddress,
"format" => "json"
],
[
"topic" => "orders/delete",
"address" => $appAddress,
"format" => "json"
]
]
]
]);
Im getting this:
POST https://smshopify.myshopify.com/admin/webhooks.json resulted in
a 422 Unprocessable Entity response: {"errors":{"topic":["can't be blank","Invalid topic specified. Topics allowed: app/uninstalled,
carts/create, carts/u (truncated...)
Is there a way to create couple webhooks in one request, I couldn't find a word about it in Shopify documentation, and my attempts to modify request body are not very successful. What I've managed to do is just foreach topics array and to the single request for every webhook.
No, there is no way to create a batch of webhooks in one request. This is true for most Shopify resources - e.g. products must also be created one-by-one.
I am using Shape Shift API for sending transaction with Guzzle. I am always getting the error given in title. My code given below:
$client = new GuzzleHttp\Client();
$data = [
"amount" => 1,
"withdrawal" => '0x23f016d7a8e408e5551ae7aa51b3fe1534165463',
"pair" => 'btc_eth',
"returnAddress" => '12stJs8vZNuuVfjZSSzpLPA96quNissk1b'
];
$result = $client->post( 'https://shapeshift.io/shift', [ 'Content-Type' => 'application/json' ],
[ 'json' => $data ] );
print "<pre>";
print_r( $result->getBody()->getContents() );
print "</pre>";
Same parameters work fine when using in Python.
You are doing the request wrong. The correct variant is with two parameters:
$result = $client->post('https://shapeshift.io/shift', ['json' => $data]);
Content type is configured automatically when you use json option.