bitpay / php-bitpay-client

PHP implementation for the BitPay cryptographically secure RESTful API
MIT License
165 stars 149 forks source link

array_key_exists() expects parameter 2 to be array, null given #279

Open Dadibom opened 6 years ago

Dadibom commented 6 years ago

Latest release sometimes gives the following error: array_key_exists() expects parameter 2 to be array, null given in Client.php (line 125)

the function fillInvoiceData receives null as its second parameter as $data = $body['data']; is null on line 206 (createInvoice)

pieterpoorthuis commented 6 years ago

When creating an invoice, either an error message or data has to be returned by BitPay.

Can you explain when this happens? Because if no data is returned, invoice creation fails and an error should be thrown as well.

petski commented 6 years ago

Just a comment to confirm this bug report. Had a couple of these between 07-Jun-2018 21:59:21 (CEST) and 07-Jun-2018 22:15:42 (CEST).

My theory is that the server returned a non-200 response, with which the code doesn't take care of in src/Bitpay/Client/Adapter/CurlAdapter.php::sendRequest()

I normally do something like this:

$res = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = $curl_errno ? curl_error($ch) : null;
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($res === false) {
  throw new ...
}

if ($status_code != 200) {
  throw new ...
}

.. etc etc
Dadibom commented 6 years ago

Sorry, forgot to respond. I don't know what causes this. It's only happened at one point for me (not personally, i found the error in my logs), a couple of attempts in a row.

peter279k commented 5 years ago

To prevent this error, I think we can check the $data is array type or casting this to array type before passing to the array_key_exists function.