Skip to content

Commit cd7a662

Browse files
committed
remove response paser trait
1 parent c3fa69b commit cd7a662

File tree

3 files changed

+29
-37
lines changed

3 files changed

+29
-37
lines changed

src/Curl.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
class Curl
66
{
7-
use ResponseParser;
8-
97
protected $id;
108
protected $handle;
119

@@ -120,7 +118,8 @@ public function exec()
120118
$errno = curl_errno($this->handle);
121119
$errstr = curl_error($this->handle);//Fix: curl_errno() always return 0 when fail
122120
$url = curl_getinfo($this->handle, CURLINFO_EFFECTIVE_URL);
123-
$this->response = $this->make($url, $responseStr, $errno, $errstr);
121+
$code = curl_getinfo($this->handle, CURLINFO_HTTP_CODE);
122+
$this->response = Response::make($url, $code, $responseStr, $errno, $errstr);
124123
return $this->response;
125124
}
126125

src/Response.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,31 @@ public function __toString()
6464
{
6565
return $this->body;
6666
}
67+
68+
public static function parse($str)
69+
{
70+
$headers = [];
71+
list($header, $body) = explode("\r\n\r\n", $str, 2);
72+
$data = explode("\n", $header);
73+
array_shift($data);//Remove status
74+
75+
foreach ($data as $part) {
76+
$middle = explode(':', $part);
77+
$headers[trim($middle[0])] = trim($middle[1]);
78+
}
79+
return [$headers, $body];
80+
}
81+
82+
public static function make($url, $code, $responseStr, $errno, $errstr)
83+
{
84+
$error = [];
85+
if ($errno || $errstr) {
86+
$headers = [];
87+
$body = '';
88+
$error = [$errno, $errstr];
89+
} else {
90+
list($headers, $body) = static::parse($responseStr);
91+
}
92+
return new static($url, $code, $body, $headers, $error);
93+
}
6794
}

src/ResponseParser.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)