Trying to update data with guzzle - php

I am using Guzzle and I need to update data, so for this I am trying the following:
$request = $this->guzzleClient->put($this->url, [
'headers' => $this->dcsheaders,
'json' => json_encode([
"status" => "Open",
"accountNumber"=> "01236548",
"reasonId"=>"ccaa8e8d-70ae-466d-b8c8-dd16bc5454e0"
])
]);
$a = $request->send();
var_dump($a);
But I receive
resulted in a 400 Bad Request response
I tried another types like form_params, but I receive 403

The json option is used to easily upload JSON encoded data as the body of a request.
source: json request option guzzle
change to this
$request = $this->guzzleClient->put($this->url, [
'headers' => $this->dcsheaders,
'json' => [
"status" => "Open",
"accountNumber"=> "01236548",
"reasonId"=>"ccaa8e8d-70ae-466d-b8c8-dd16bc5454e0"
]
]);

Related

param is missing or the value is empty laravel

I am creating a user with the api provided. I am using Laravel and trying to store data to smartrmail and docs to create new subscriber is here https://docs.smartrmail.com/en/articles/636615-list-subscribers
Each time i send request i get following error:
Server error: `POST https://go.smartrmail.com/api/v1/lists/1sptso/list_subscribers` resulted in a `500 Internal Server Error` response: {"error":"param is missing or the value is empty: subscribers"}
{"error":"param is missing or the value is empty: subscribers"}
I am using Laravel and my code is here
Route::get('smartrmail',function(){
$headers = [
'Accept' => 'application/json',
'Authorization' => 'token f91715d5-3aac-4db3-a133-4b3a9493a9a4',
'Content-Type' => 'application/json',
];
$client = new GuzzleHttp\Client([
'headers' => $headers
]);
$data = [
"subscribers"=>[
[
"email"=> "vanhalen#example.com",
"first_name"=> "van",
"last_name"=> "halen",
"subscribed"=> true,
]
]
];
$res = $client->request('POST', 'https://go.smartrmail.com/api/v1/lists/1sptso/list_subscribers', [
'form_params' => [
$data
]
]);
return($res);
// echo $res->getStatusCode();
});
Anybody help me to figure out what is wrong here. I am following this docs
https://docs.smartrmail.com/en/articles/636615-list-subscribers
to create a new subscriber
Instead of
'form_params' => [
$data
]
use
'json' => $data
Explanation
You want to send json data (I assume that because you set header 'Content-Type' => 'application/json', which means that you are sending json), but form_params is used for application/x-www-form-urlencoded.
json sets header to application/json and sends data as json.
As you set proper header, this should work too:
'body' => $data
Proper name of param you can find in Guzzle docs, I used uploading data part.

How can I use Guzzle to send a PUT request?

Does anybody know the correct way to PUT using Guzzle? my code is not working
but my post methods are working
$enrolment = $client->request('PUT', $url,[
'form_params' => [
'contactID' =>12345,
'type' =>'w'
],
'headers' => [
'apitoken' => $api_token,
'wstoken' => $ws_token
]
]);
resulted in a 500 Internal Server Error response:↵{"DATA":"","ERROR":true,"MESSAGES":"key [TYPE] doesn't exist","CODE":"0","DETAILS":""}
The PUT request does't accept form_params type as request option, so it may ignore the setting.
From Docs:
form_params
Used to send an application/x-www-form-urlencoded
POST request.
Maybe you can try using json for PUT request.
In the json part of Docs, it uses PUT as well.
$enrolment = $client->request('PUT', $url,[
'json' => [
'contactID' =>12345,
'type' =>'w'
],
'headers' => [
'apitoken' => $api_token,
'wstoken' => $ws_token
]
]);

Guzzle 6 PUT request not sending form params

I have the following code that I use whenever I want to make POST requests using Guzzle:
$request = $client->request('POST', $url, [
'form_params' => $params,
'headers' => [
'Referer' => '(intentionally removed)',
'Accept' => 'application/json',
]
]);
The code works without any issues and the information within $params is always sent, however when I change the request type from POST to PUT so that the request becomes:
$request = $client->request('PUT', $url, [
'form_params' => $params,
'headers' => [
'Referer' => '(intentionally removed)',
'Accept' => 'application/json',
]
]);
The request suddenly stops sending the data contained within $params.
I have tested the endpoint the request is send to with Insomnia with both POST and PUT requests and both types are processed as expected, so I am certain the issue is not there.
What can be causing the data from Guzzle to be send using the POST method but not when using the PUT?
This behaviour described in guzzle documentation form-params
form_params - Used to send an application/x-www-form-urlencoded POST request.
Probably, you enough pass the parameters in json format:
$request = $client->request('PUT', $url, [
'json' => $params,
'headers' => [
'Referer' => '(intentionally removed)',
'Accept' => 'application/json',
]
]);

Shopify API - create webhook (batch request)

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.

How to send data to CloudFlare API?

I'm trying to delete files from my CloudFlare cache using PHP. Using Guzzle I've done this:
$client = new \GuzzleHttp\Client;
$response = $client->delete('https://api.cloudflare.com/client/v4/zones/myzoneid/purge_cache', [
'query' => [
'files' => 'https://example.com/styles.css,
],
'headers' => [
'X-Auth-Email' => 'myemail',
'X-Auth-Key' => 'myapikey',
],
]);
But when I run this I get an error:
Client error: DELETE https://api.cloudflare.com/client/v4/zones/myzoneid/purge_cache?files=https%3A%2F%2Fexample.com/etc resulted in a 400 Bad Request response: {"success":false,"errors":[{"code":1012,"message":"Request must contain one of \"purge_everything\", \"files\", \"tags\" (truncated...)
I can't get it to work using Postman either. I put in the required headers and try to set a key of files or files[] with the URL but it doesn't work. I've also tried data with raw JSON as the value like {"files":["url"]} (along with a JSON content-type header) but get the same error. It thinks I'm not sending the files key.
The method for purge_cache is POST instead of DELETE (Source: https://api.cloudflare.com/#zone-purge-files-by-url).
The payload is not sent as 'query', but as 'json'.
Files should be an array, not a string.
So the correct syntax should be....
$client = new \GuzzleHttp\Client;
$response = $client->post('https://api.cloudflare.com/client/v4/zones/myzoneid/purge_cache', [
'json' => [
'files' => ['https://example.com/styles.css'],
],
'headers' => [
'X-Auth-Email' => 'myemail',
'X-Auth-Key' => 'myapikey',
],
]);

Categories