How i can use variables on json with Guzzle? - php

I'm trying to use variables in php to add values to the json on Guzzle, but that appear that's not working, i'm doing it wrong? Thanks in advance, im new in the use of Guzzle. (Guzzle 6)
$send_response = $send->post("https://yyy.com/services/" , [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' .$token
],
'json' => [
'key' => $value,
'key2' => $value2,
]
]);

Related

Create an asset programmatically Azure Media Setrvice v3 PHP

I'm trying to create an asset with the Guzzle client using this code
$response = $this->httpClient->request('PUT', $request_url, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->getAccessToken(),
],
'form_params' => [],
]);
I'm getting this error
400 Bad Request` response: { "error": { "code": "InvalidResource", "message": "The input resource must be specified in the request bod (truncated...) in GuzzleHttp\Exception\RequestException::create() (line 113 of /var/www/html/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php).
I was able to create assets with postman using the same credentials and tokens.
The error message seems to point to the missing storage account name in the body.
https://learn.microsoft.com/en-us/rest/api/media/assets/create-or-update?tabs=HTTP#create-an-asset
Can you check if body is being sent correctly.
I won't delete the question so while searching I found a solution you need to send a body like this in order to create an asset.
$response = $this->httpClient->request('PUT', $request_url, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->getAccessToken(),
],
'json' => [
'properties' => [
'description' => ''
]
]
]);

How to send Fast Excel to Guzzle post request

i use Fast Excel library from https://github.com/rap2hpoutre/fast-excel, can i put return from ->download('file.xlsx') to Guzzle post request? with this code i'm not get anything
$httpClient = new \GuzzleHttp\Client([
'headers' => [
'Authorization' => mdmTokenGenerate($token),
'Accept' => 'application/json'
],
'verify' => false
]);
$response = $httpClient->post(
"{$client->domain}/api/push", [
'multipart' => [
[
'name' => 'file',
'contents' => (new FastExcel(MasterNumbers::limit(1000)->get()))->download('file.xlsx')
]
]
]
);

Illuminate\Routing\Exceptions\UrlGenerationException: Missing required parameters for

I'm using Laravel 6.2. I have a named route
Route::get('/dummy/{id}', 'Api\V1\DummyDataController#show')->name('dummy_data_show');
I cannot write the test for it, I get the error Illuminate\Routing\Exceptions\UrlGenerationException: Missing required parameters for [Route: dummy_data_show] [URI: api/v1/dummy/{id}].
These are my attempts (only relevant code):
$request = $this->withHeaders(
[
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
]
)->json('GET',
Route('dummy_data_show'),
[
'id' => 1,
]
);
and also
$request = $this->withHeaders(
[
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
]
)->json('GET',
Route('dummy_data_show'),
1
);
Of course if I try with
$request = $this->withHeaders(
[
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
]
)->json('api/v1/dummy/1');
I don't get the error. What is my error? Thank you!
You are not passing any parameters to your route method. The parameters should be inside the parenthesis.
Change:
Route('dummy_data_show'),
[
'id' => 1,
]
To:
route('dummy_data_show', [
'id' => 1,
])

PHP Guzzle POST Request Returning Null

I'm racking my brains over this - the request goes fine in Postman, but I can't access the body using Guzzle.
$client = new \GuzzleHttp\Client();
$res = $client->request('POST', 'https://apitest.authorize.net/xml/v1/request.api', [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode([
'createTransactionRequest' => [
'merchantAuthentication' => [
'name' => '88VuW****',
'transactionKey' => '2xyW8q65VZ*****'
],
'transactionRequest' => [
'transactionType' => 'authCaptureTransaction',
'amount' => "5.00",
'payment' => [
'opaqueData' => [
'dataDescriptor' => 'COMMON.ACCEPT.INAPP.PAYMENT',
'dataValue' => $_POST['dataValue']
]
]
]
]
])
]);
dd(json_decode($res->getBody()));
The above returns null - no errors, just null. The below is the same request in Postman, successful with a body.
Any ideas would be really appreciated, thanks!
You should use $res->getBody()->getContents().
And json_decode() returns null in case of any error. You have to check them separately (unfortunately) with json_last_error().

Guzzle 6 send multipart data

I'd like to add some data to a Guzzle Http Request. There are file name, file content and header with authorization key.
$this->request = $this->client->request('POST', 'url', [
'multipart' => [
'name' => 'image_file',
'contents' => fopen('http://localhost:8000/vendor/l5-swagger/images/logo_small.png', 'r'),
'headers' =>
['Authorization' => 'Bearer uCMvsgyuYm0idmedWFVUx8DXsN8QzYQj82XDkUTw']
]]);
but I get error
Catchable Fatal Error: Argument 2 passed to GuzzleHttp\Psr7\MultipartStream::addElement() must be of the type array, string
given, called in vendor\guzzlehttp\psr7\src\MultipartStream.php on line 70 and defined in vendor\guzzlehttp\psr7\src\MultipartStream.php line 79
In Guzzle 6 documentation is something like this: http://docs.guzzlephp.org/en/latest/request-options.html#multipart
Who knows where I made a mistake?
Here is the solution. Header with access token should be outside multipart section.
$this->request = $this->client->request('POST', 'request_url', [
'headers' => [
'Authorization' => 'Bearer access_token'
],
'multipart' => [
[
'Content-type' => 'multipart/form-data',
'name' => 'image_file',
'contents' => fopen('image_file_url', 'r')
]
]
]);
As per the docs, "The value of multipart is an array of associative arrays", so you need to nest one level deeper:
$this->request = $this->client->request('POST', 'url', [
'multipart' => [
[
'name' => 'image_file',
'contents' => fopen('http://localhost:8000/vendor/l5-swagger/images/logo_small.png', 'r'),
'headers' => ['Authorization' => 'Bearer uCMvsgyuYm0idmedWFVUx8DXsN8QzYQj82XDkUTw']
]
]
]);
try this
works for me
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Utils;
$this->client = new Client([
'base_uri' => 'https://baseurl'
]);
$body = Utils::tryFopen($tempPath . $fileName, 'r');
$res = $this->client->request(
'POST',
'url',
[
'headers' => [
...
],
'body' => $body
]
);

Categories