8000 Extract _parseApiHeaders() method · redaxeprogrammers/codebird-php@331d08f · GitHub
[go: up one dir, main page]

Skip to content

Commit 331d08f

Browse files
committed
Extract _parseApiHeaders() method
1 parent be720fd commit 331d08f

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/codebird.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,9 @@ protected function _oauth2TokenCurl()
665665
$this->_validateSslCertificate($validation_result);
666666

667667
$httpstatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
668-
$reply = $this->_parseApiReply($result);
669-
$headers = $this->_parseApiReply($result, true);
670-
$rate = $this->_getRateLimitInfo($headers);
668+
list($headers, $reply) = $this->_parseApiHeaders($result);
669+
$reply = $this->_parseApiReply($reply);
670+
$rate = $this->_getRateLimitInfo($headers);
671671
switch ($this->_return_format) {
672672
case CODEBIRD_RETURNFORMAT_ARRAY:
673673
$reply['httpstatus'] = $httpstatus;
@@ -745,9 +745,9 @@ protected function _oauth2TokenNoCurl()
745745
$httpstatus = $match[1];
746746
}
747747

748-
$reply = $this->_parseApiReply($result);
749-
$headers = $this->_parseApiReply($result, true);
750-
$rate = $this->_getRateLimitInfo($headers);
748+
list($headers, $reply) = $this->_parseApiHeaders($result);
749+
$reply = $this->_parseApiReply($reply);
750+
$rate = $this->_getRateLimitInfo($headers);
751751
switch ($this->_return_format) {
752752
case CODEBIRD_RETURNFORMAT_ARRAY:
753753
$reply['httpstatus'] = $httpstatus;
@@ -1427,9 +1427,9 @@ protected function _callApiNoCurl($httpmethod, $method, $params = array(), $mult
14271427
$httpstatus = $match[1];
14281428
}
14291429

1430-
$reply = $this->_parseApiReply($result);
1431-
$headers = $this->_parseApiReply($result, true);
1432-
$rate = $this->_getRateLimitInfo($headers);
1430+
list($headers, $reply) = $this->_parseApiHeaders($result);
1431+
$reply = $this->_parseApiReply($reply);
1432+
$rate = $this->_getRateLimitInfo($headers);
14331433
switch ($this->_return_format) {
14341434
case CODEBIRD_RETURNFORMAT_ARRAY:
14351435
$reply['httpstatus'] = $httpstatus;
@@ -1444,15 +1444,13 @@ protected function _callApiNoCurl($httpmethod, $method, $params = array(), $mult
14441444
}
14451445

14461446
/**
1447-
* Parses the API reply to encode it in the set return_format
1447+
* Parses the API reply to separate headers from the body
14481448
*
1449-
* @param string $reply The actual reply, JSON-encoded or URL-encoded
1450-
* @param bool $get_headers If to return the headers instead of body
1449+
* @param string $reply The actual raw HTTP request reply
14511450
*
1452-
* @return array|object The parsed reply
1451+
* @return array (headers, reply)
14531452
*/
1454-
protected function _parseApiReply($reply, $get_headers = false)
1455-
{
1453+
protected function _parseApiHeaders($reply) {
14561454
// split headers and body
14571455
$headers = array();
14581456
$reply = explode("\r\n\r\n", $reply, 4);
@@ -1481,15 +1479,25 @@ protected function _parseApiReply($reply, $get_headers = false)
14811479
}
14821480
$headers[$key] = $value;
14831481
}
1484-
if ($get_headers) {
1485-
return $headers;
1486-
}
1482+
14871483
if (count($reply) > 1) {
14881484
$reply = $reply[1];
14891485
} else {
14901486
$reply = '';
14911487
}
14921488

1489+
return array($headers, $reply);
1490+
}
1491+
1492+
/**
1493+
* Parses the API reply to encode it in the set return_format
1494+
*
1495+
* @param string $reply The actual HTTP body, JSON-encoded or URL-encoded
1496+
*
1497+
* @return array|object The parsed reply
1498+
*/
1499+
protected function _parseApiReply($reply)
1500+
{
14931501
$need_array = $this->_return_format === CODEBIRD_RETURNFORMAT_ARRAY;
14941502
if ($reply === '[]') {
14951503
switch ($this->_return_format) {

0 commit comments

Comments
 (0)
0