php-mod / curl

This library provides an object-oriented and dependency free wrapper of the PHP cURL extension.
MIT License
328 stars 120 forks source link

Problem GET with params #102

Closed themegass closed 1 year ago

themegass commented 1 year ago

Hi

I was testing your code and I found a problem.

Basically, I amb using GET with multiple parameters:

$client->get($method_url, $data);

where data is some like:

 $data = array("module_id" => 1, "module_instance" => 2)

When I call to get the built URL is:

HTTP GET /api/course/87/activity/?module_id=1&module_instance=2 

But the REST API should get:

HTTP GET /api/course/87/activity/?module_id=1&module_instance=2 

It looks like a problem with:

http_build_query($data) 

When I change with:

str_replace("&","&",http_build_query($data,"&"))

It works

Can you help me to understand my error?

Thanks!!

nadar commented 1 year ago

The class utilizes the http_build_query function for get requests here https://github.com/php-mod/curl/blob/master/src/Curl/Curl.php#L310, which should return &. You can test this here: https://3v4l.org/35rNL

So i am curious why you get & instead of expected &. What php version did you use? Is there any other code parts involved? Did you made a var_dump of the url in the code?

I can make some tests in order to verify the behavior.

nadar commented 1 year ago

There is also an ini value for the default arg seperator, i was not aware of this. Maybe check your php ini config => https://www.php.net/manual/en/ini.core.php#ini.arg-separator.output

themegass commented 1 year ago

Hi

Thanks for the quick response

The code is inside a Moodle 4.0.1 docker machine.

As you said, it may be the ini parameter is not well configured in that machine.

I will check this parameter in the docker machine.

I will return to you after checking

Thanks!!

themegass commented 1 year ago

Hi again

You were correct.

The php.ini is set to: arg_separator.output = "\&"

I guess that this is configured such way for Moodle functionality (I am not an expert on how Moodle works).

The problem is how to force to curl connections to other servers (or other docker containers)

Do you have any suggestions on how to solve this?

Thanks!!

nadar commented 1 year ago

Then you have two options:

  1. Change the arg_separator.output to & :-) would be the simplest solution.
  2. add the params by yourself to the base path:
$url = '/api/course/87/activity/?' . http_build_query(["module_id" => 1, "module_instance" => 2], '', '&');
$client->get($url);