#withQueryParameters

44 messages · Page 1 of 1 (latest)

graceful crest
#

Hi all, thanks for taking the time to look at my post :)

im fairly new to PHP and Laravel, im trying to make a URL with query paramaters, to call an API:

$devices = MyModel::pluck('uuid')->all(); here i'm pulling a single column from my table and then trying to generate my URL string like this:

               'Authorization' => "Bearer $accessToken",
           ])->withQueryParameters(['devicesUuids' => $chunk ])->get(
               "https://euabc.cdb",
           );

however my string is returning like so:
https://euabc.cdb?devicesUuids%5B0%5D=abc?devicesUuids5B0%5D=thd&devicesUuids5B0%5D=123

when i print out my array it display's like:

[0] => value
[1] => value
[2] => value

has anyone got any advice how i can remote the %5B0%5D which i think is [ ]

dreamy bobcat
#

you're correct: %5B0%5D is [].

you sure you want to remove it? that's how usually multiple values in the same key are parsed in query string, either that or empty arrays like ?key[]=value&key[]=value.

if you really don't need that just taking the string and maniplating it for removing the %5B0%5D should work

graceful crest
#

The API im trying to hit is .net and from the example request they've given me they're not there

elfin geode
#

The same query parameter name is just repeated?

graceful crest
#

Yea

elfin geode
#

In that case, you need to build the URL yourself, or remove the [] part

#

E.g. using http_build_query, or just a loop

graceful crest
#

Going by this, I don't understand why the default is with the brackets

elfin geode
#

There seems to be no standard way, but HTML forms does the param[], so php chose that path.

graceful crest
#

ok im getting the same results when doing this:


            $queryParams = http_build_query(['devicesUuids' => $chunk]);```
elfin geode
#

But in that image, there is no array data, so that says absolutely nothing about your use case

graceful crest
#

no thats true but thats what im trying to achive, sorry i think i confused the question abit ignore that image

elfin geode
#

If the docs says to not have [], and having [] for array data doesn't work, then you need to build a URL without the [] part. E.g. using http_build_query and then removing it.

graceful crest
#

was trying to not traverse back over it and do it on the fly, okay i'll give it ago seems either way i try it i get the same problem. well it's not a problem it's obviusly normal behaviour

#

i oringinally thought it was because when i print my array it appears as index - value and i thought it was grabbing the index

elfin geode
#

You just need to build a string a=...&a=...&a=...? Doesn't seem like a difficult programming task

graceful crest
#

no it's not; since im new to PHP i thought i was doing something wrong is all

elfin geode
#

But i'd still recommend using http_build_query and then just replace all [], to ensure you get all encoding correct (and, it's two lines of code)

graceful crest
#

thank you for the advice, now i know this is normal behaviour

elfin geode
#

I have never seen a=...&a=...&a=..., so not very normal to me, but if the API docs says do to so, then you need to do so

#

(Assuming, again, you have verified the [] is the problem)

graceful crest
#

well not yet, all i get back right now is a support error and the company takes ages to reply. once i verify it in a second i'll know for sure

cyan lily
#

@graceful crest what exactly do you want to do? Do you want deviceUuids to appear multiple times, or do you want the array value to be flattened to a string?

graceful crest
#

so im calling a API, that returns me back data based on uuid as a query paramter, it can accept multiple ones...

so the doco's says it should look like:

#

<url>?devicesUuids=string1&devicesUuids=string2 calling two devices

cyan lily
#

Ah, yeah PHP won't do that natively

#

Because it uses an array to store it, and each subsequent param would overwrite the previous

#

You can't do what you need to do with the Laravel HTTP client unfortunately

#

As the convention in PHP is to do that stupid deviceUuids[] stuff

graceful crest
#

yes looking at the string and tryng to call 30 of these bad boi's it does look like it's incrementing it up [1] - [2] when i just tried the str_replace i didnt notice it was incrementing xD

#

now i see it

#

tbh i thought it was because when i cal Array in laravel, it prints out as

[1] => value
[2] => value

so today i was trying to flatten it out to just a plain string array [value, value]

cyan lily
#

It's definitely a PHP thing rather than Laravel, though there isn't a way to do what you need with Laravel without doing some really hacky stuff

graceful crest
#

always great when your exploring new frameworks

#

this damn backend API call is only going to call every 120 seconds to pull and data for me to display, turning into abit of a horror show; anyway if Http isnt going to do this, do you have a suggestion another way?

elfin geode
#

Here's a very simple implementation, if you don't need to worry about encoding (I'm guessing the UUIDs are alphanumeric)

> $query = implode('&', array_map(fn ($uuid) => "deviceUuids=$uuid", $uuids))
= "deviceUuids=123&deviceUuids=456&deviceUuids=abc&deviceUuids=def"
graceful crest
#

the uuid's are exactly that uuid, so just a jumble 0e4bge15d-bd363-4a51-ad7b-792cf25f6d40 for example

elfin geode
#

Yes, exactly

#

You'll use Http::, you just have to compute the URL first

#

And just ->get("https://euabc.cdb?$query")

graceful crest
elfin geode
#

How many uuids will you have?

graceful crest
#

194 but im using chunk to do 30 at a time

elfin geode
#

Watch out for generating too long URLs, some systems have length limits, like 4k