anlutro / php-curl

Simple PHP curl wrapper class

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cURL.php prepareRequest - wrong behavior

akoevroman opened this issue · comments

commented

In several cases: $request->hasData() return true for empty(default) array of data.
This happens if $request->encodeData() return string '[]'
In this case, empty CURLOPT_POSTFIELDS are sent.

Also Request.php -> encodeData function has $encoding variable which is not defined.

I recently implemented this behaviour so maybe I can help.

This is important for JSON communication, because empty arrays and empty objects are encoded to '[]' and '{}' respectively, and this is NON-empty data, hasData MUST be true, and CURLOPT_POSTFIELDS MUST be sent.

Please state your exact usage, the expected result, and the actual result, so that we may find a universal solution.

What @djcvijic wrote is indeed true!

@akoevroman what version of the library are you using? This should be fixed in 1.4.5 or higher.

I pushed a commit for the $encoding variable issue you mentioned, though that default case should never actually trigger so I don't think it's a fatal bug.

commented

Version 1.4.6

Example:

$request = cURL::newJsonRequest('get', $this->requestUrl);
$response = $request->send();

so then we go to lib

public function newJsonRequest($method, $url, $data = array())
	{
		return $this->newRequest($method, $url, $data, Request::ENCODING_JSON);
	}

array() by default

then
public function newRequest($method, $url, $data = array(), $encoding = Request::ENCODING_QUERY)
also array

Finaly:

array->
$request->setData($data);
->
$this->data = $data;
->
$request->hasData()
-> '[]' returned

maybe... i did something wrong?

Of course, "'[]' returned" is not a critical error but some API denied empty array in POST fields and return a response that appropriate data are expected.

P.S. Maybe if I will use 3rd parameter and set it to '' it will work, but this is not obvious logic. And i did not try it.

That's an annoying bug. See the commit message of a2809a2 for more information. I've tagged 1.4.7 with the fix.

Yes, the recent fix I made was to cover POST-like request methods, but then GET was broken in the process. 1.4.7 looks good to me.