maybe someone has experienced the same problem, I want to make a GET request by sending data in the url parameter,
the endpoint need request like this:
https://test.com/sales/transaction/?page=1&pageSize=1&transactionDateFrom=2023-01-24T00:00:00.000Z&transactionDateTo=2023-01-24T15:59:59.000Z
but if i send from guzzle using dynamics data, the request is like this:
https://test.com/sales/transaction/?page=1&pageSize=5&transactionDateFrom=2023-01-2500%3A00%3A00.000Z&transactionDateTo=2023-01-2523%3A59%3A59.000Z
my code to send request:
$req = $client->request('GET','https://test.com/sales/transaction/', [
'query' => [
'page' => 1,
'pageSize' => 5,
'transactionDateFrom' => 2023-01-24T00:00:00.000Z,
'transactionDateTo' => 2023-01-24T15:59:59.000Z
],
'headers' => $headers
]);
how to resolve the timezone format to normal string like this
transactionDateFrom=2023-01-24T00:00:00.000Z&transactionDateTo=2023-01-24T15:59:59.000Z
maybe someone has solved this problem before and can provide some information, thanks
Related
I am trying to send multiple posts to Twilio using Guzzle. My apologizes, I'm very new to guzzle. I have seen some examples where I can set an array of URIs and run the posts in parallel. But I haven't been able to figure out how to use the same URI with different parameters for each request.
The ONLY difference between each call would the the "TO" field. The body, messageSID, and auth would stay the same for each parallel call. I wanted to get this test working then eventually just be able to build the array of anywhere from 1 to 100 TO phone numbers.
Here is my code to send one request:
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
$client = new Client(["base_uri" => "https://api.twilio.com/2010-04-01/Accounts/"]);
$options = array(
'form_params' => [
"Body" => "hello world",
"To" => "+12015551234",
"MessagingServiceSid" => "MGxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
],
'auth' => [
"accountsidxxxxxxxxxxx",
"tokenxxxxxxxxxxxxxxx"
]
);
$response = $client->post("ACxxxxxxxxxxxxxxxxxxxxx/Messages.json", $options);
echo $response->getBody();
I've two working project communicating between each other via an API built in a Laravel. So far there is only simple POST requests made with GuzzleHttp 6.
And I am currently trying to have a new POST request made from 1 to 2, which would send a couple of simple fields along with one file.
Project 1 has a form, on the form submit I handle the data and want to send them to project 2 via a POST request to this new API endpoint.
I've tried different guzzle options 'multipart', 'form_data' etc and realised they may not be combined together. Now I understood that this options are exclusive and using only "multipart" seems the way to go.
But when I send my request to Laravel no data nor file are there.
Here is the code for my request
$options = [
'multipart' =>
[
[
'name' => 'data',
'contents' => '{"field_1":"Test","field_2":"Test","field_3":"Test"}',
'headers' =>
[
'Content-Type' => 'application/json',
],
],
[
'name' => 'file',
'filename' => 'test.pdf',
'Mime-Type' => 'application/pdf',
'contents' => file_get_contents($_FILEs['text_file']['temp_name']),
]
]
];
$this->client->request('POST', "api/test_post", $options)
I also gve this a try:
$options = [
'multipart' =>
[
[
'name' => 'field_1',
'contents' => 'Test',
],
[
'name' => 'field_2',
'contents' => 'Test',
],
[
'name' => 'file',
'filename' => 'test.pdf',
'Mime-Type' => 'application/pdf',
'contents' => fopen($_FILEs['text_file']['temp_name'],'r'),
]
]
];
$this->client->request('POST', "api/test_post", $options)
If I look the request content on the receiving end, nothing is there. No field or file.
I've seen couples posts, some say to include headers some say not too. I kinda got lost and amd now running out of ideas.
I would expecet the infos to be as if they where form post I guess:
$request->inpust('field_1') -> 'test'
$request->inpust('field_2') -> 'test'
$request->inpust('field_3') -> 'test'
$request->file('file') -> my uploaded file
Also I should point out that I am not exactly sure how multipart/form-data works, so that might not help me.
If you can point me to the right direction, that would help a lot
Well I finally figured it out. The second example from above is the way to go also be sure to check the headers of the request and the client...
As this API has been running for quite some time and was only doing json type requests, the Client was instantiated with
$options = [
headers => [ 'Content-Type' => 'application/json']
]
Which, as stated in multiple answers across the internet, prevents Guzzle to automatically set the Content-Type depending of the request options.
In my case, removing this line made Guzzle enable to set it properly when provided with 'multipart' option.
Also, as all other requests are using the 'json' options, Guzzle also works it's magic and set 'Content-Type' => 'application/json' as well.
I'm using an api that has a "range" parameter that can apply to several different parameter items. The range i'm focusing on is "price". I'm using guzzle in laravel and according to the api documentation, the query for this particular parameter should be written like this "&range_facet=price|500|2500|250"...this is broken down into the minimum, maximum, and interval values of the price range parameter. That's not necessarily important to this question. When i try and run this query as is, i get nothing returned. When I remove that particular parameter, i get values but obviously they're not filtered the way i want them to be. When i run this in Insomnia, the pipes are replaced by "%7C", which is obviously (obviously?) not interpreted by the api as it's not how it's waiting for the GET request to be made. How can I insert the pipes into the query so that it calls the correct way?
I've tried to create an additional nested array with the price value being broken up into key value pairs but that didn't work either.
'range_facets' => ['price'['start'=>'500', end=>'2500', 'interval'=>'250']],
$client = new Client();
$result = $client->request('GET', "http://api.example.com", [
'headers' => [
'Host' => 'example-host',
'Content-Type' => 'application/json'
],
'query' => [
'api_key' => 'my_api_key',
'range_facets' => 'price|500|2500|250',
'year' => $year,
'latitude' => '30.170222',
'longitude' => '92.01320199',
'radius' => 500,
'start' => 0,
'rows' => 50
]
]);
I'd like to filter my prices but I need the pipe to be able to do it.
This is exactly how it should be. %7C should be decoded on the server side to | automatically (about query string encoding).
I bet the issue is in different place.
This is the example adobe have in their documentation.
I have tried with Guzzle:
$client->request('POST', 'transientDocuments', [
'multipart' => [
[
'name' => 'test',
'contents' => fopen('pdfs/test.pdf', 'r'),
'filename' => 'file.pdf',
'headers' => [
'Content-Type' => 'multipart/form-data',
'Content-Transfer-Encoding' => "binary",
]
],
]
]);
fopen returns resource(13) of type (stream)
But every time I get {"code":"NO_FILE_CONTENT","message":"Must provide file body"}.
You should write code to satisfy the following request shown in image below.
Explanation
When u log in with your e-sign account you will be redirected to one of the hosts (like api.in1.echosign.com). You need to put File as form-data parameter key and your file as value. You get Mime-Type based on file extension. In response you will get TransientDocumentId which can be used for the creating and sending agreements etc.
Was stuck with the same but realised that the parameters name and filename to be enclosed in double quotes.
'name' => '"File"',
'filename' => '"file.pdf"',
PS. my code is in .Net, not php
I am trying top use guzzle to get data from https://idf.intven.com/public_patents. The page loads data with AJAX by making request to https://idf.intven.com/public_patent_listing.json.
I am working on a another site for them that they want to use this data for so I am trying to grab this data with guzzle but I keep getting 500 errors.
$this->client = new Client();
$this->client->post( 'https://idf.intven.com/public_patent_listing.json', [
'verify' => false,
'json' => [
"report_type" => "public_patent_listing",
"queryFields" => [],
"filters" => [],"per_page" => 16,
"from" => 0,
"sort" => "issued_on",
"sort_order" => "desc"
],
]);
Maybe because you are using arrays instead of json objects. Change the square brackets to curly ones for main object and json object in it