linslin/Yii2-Curl

Undefined offset: 1

battle-mage opened this issue · 5 comments

We are facing a problem since the last update while Curl is trying to extract additional params.
https://github.com/linslin/Yii2-Curl/blob/master/Curl.php:

if (preg_match('~^charset=(.+?)$~', trim($possible_charset), $matches) && isset($matches[1])) {
    $this->responseCharset = strtolower($matches[1]);
}

shouldn't we use array_key_exists(1,$matches) instead of isset($matches[1])?

Using isset() or array_key_exists should be make no difference here. The only difference is that array_key_exists return false if the value is null where isset() will return true.

Please add a trace log and your controller logic for debugging

Exception with stack trace

"name":"PHP Notice",
"message":"Undefined offset: 1",
"code":8,
"type":"yii\\base\\ErrorException",
"file":"/var/project/yii2backend/vendor/linslin/yii2-curl/Curl.php",
"line":405,
"stack-trace":[
"#0 /var/project/yii2backend/vendor/linslin/yii2-curl/Curl.php(405): yii\\base\\ErrorHandler->handleError(8, 'Undefined offse...', '/var/project/yi...', 405, Array)",
"#1 /var/project/yii2backend/vendor/linslin/yii2-curl/Curl.php(374): linslin\\yii2\\curl\\Curl->_extractAdditionalCurlParameter()",
"#2 /var/project/yii2backend/vendor/linslin/yii2-curl/Curl.php(129): linslin\\yii2\\curl\\Curl->_httpRequest('POST', 'https://domain....', false)",
"#3 /var/project/yii2backend/components/user/UserNotificationServiceImpl.php(49): linslin\\yii2\\curl\\Curl->post('https://domain....', false)",
"#4 /var/project/yii2backend/components/user/UserNotificationServiceImpl.php(84): app\\components\\user\\UserNotificationServiceImpl->sendEmail('test@example....', '\\xD0\\xA0\\xD0\\xB5\\xD0\\xB3\\xD0\\xB8\\xD1\\x81\\xD1\\x82\\xD1\\x80\\xD0...', Array, 'registration_ge...')",
"#14 /var/project/yii2backend/web/index.php(39): yii\\base\\Application->run()",
"#15 {main}"
]

I found out that actually this row causes the exception:
list($this->responseType, $possible_charset) = explode(';', curl_getinfo($this->_curl, CURLINFO_CONTENT_TYPE));

Values of my variables at this moment:
$this->responseType = null
$possible_charset = undefined

Result of explode(';', curl_getinfo($this->_curl, CURLINFO_CONTENT_TYPE)):
array(1) { [0]=> string(16) "application/json" }

Okey, the problem is a PHP notice due to list(). Could you checkout the dev branch and check if its gone? If so i would release v1.0.10.

Yes, dev 1.0.10 seem to be working fine.
The reason of problem is that I had only one header, while list() expected at least two elements?

Yea, list() comes with a lot of funny stuff http://php.net/manual/en/function.list.php. Version 1.0.10 is released. https://github.com/linslin/Yii2-Curl/releases/tag/1.0.10

Thank you.