Skip to content

Commit b2435aa

Browse files
committed
Move response parsing to separate function
1 parent 75bc90f commit b2435aa

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

src/Curl.class.php

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,36 @@ private function parseRequestHeaders($raw_headers)
279279
return $request_headers;
280280
}
281281

282+
private function parseResponse($response)
283+
{
284+
$response_headers = '';
285+
if (!(strpos($response, "\r\n\r\n") === false)) {
286+
list($response_header, $response) = explode("\r\n\r\n", $response, 2);
287+
if ($response_header === 'HTTP/1.1 100 Continue') {
288+
list($response_header, $response) = explode("\r\n\r\n", $response, 2);
289+
}
290+
$response_headers = $this->parseResponseHeaders($response_header);
291+
292+
if (isset($response_headers['Content-Type'])) {
293+
if (preg_match('/^application\/json/i', $response_headers['Content-Type'])) {
294+
$json_obj = json_decode($response, false);
295+
if (!is_null($json_obj)) {
296+
$response = $json_obj;
297+
}
298+
} elseif (preg_match('/^application\/rss\+xml/i', $response_headers['Content-Type']) ||
299+
preg_match('/^application\/xml/i', $response_headers['Content-Type']) ||
300+
preg_match('/^text\/xml/i', $response_headers['Content-Type'])) {
301+
$xml_obj = @simplexml_load_string($response);
302+
if (!($xml_obj === false)) {
303+
$response = $xml_obj;
304+
}
305+
}
306+
}
307+
}
308+
309+
return array($response_headers, $response);
310+
}
311+
282312
private function parseResponseHeaders($raw_headers)
283313
{
284314
$response_headers = new CaseInsensitiveArray();
@@ -336,30 +366,7 @@ protected function exec($_ch = null)
336366
$ch->error_code = $ch->error ? ($ch->curl_error ? $ch->curl_error_code : $ch->http_status_code) : 0;
337367

338368
$ch->request_headers = $this->parseRequestHeaders(curl_getinfo($ch->curl, CURLINFO_HEADER_OUT));
339-
$ch->response_headers = '';
340-
if (!(strpos($ch->response, "\r\n\r\n") === false)) {
341-
list($response_header, $ch->response) = explode("\r\n\r\n", $ch->response, 2);
342-
if ($response_header === 'HTTP/1.1 100 Continue') {
343-
list($response_header, $ch->response) = explode("\r\n\r\n", $ch->response, 2);
344-
}
345-
$ch->response_headers = $this->parseResponseHeaders($response_header);
346-
347-
if (isset($ch->response_headers['Content-Type'])) {
348-
if (preg_match('/^application\/json/i', $ch->response_headers['Content-Type'])) {
349-
$json_obj = json_decode($ch->response, false);
350-
if (!is_null($json_obj)) {
351-
$ch->response = $json_obj;
352-
}
353-
} elseif (preg_match('/^application\/rss\+xml/i', $ch->response_headers['Content-Type']) ||
354-
preg_match('/^application\/xml/i', $ch->response_headers['Content-Type']) ||
355-
preg_match('/^text\/xml/i', $ch->response_headers['Content-Type'])) {
356-
$xml_obj = @simplexml_load_string($ch->response);
357-
if (!($xml_obj === false)) {
358-
$ch->response = $xml_obj;
359-
}
360-
}
361-
}
362-
}
369+
list($ch->response_headers, $ch->response) = $this->parseResponse($ch->response);
363370

364371
$ch->http_error_message = '';
365372
if ($ch->error) {

0 commit comments

Comments
 (0)