Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b74c3c7

Browse files
committed
updated script
1 parent c91a289 commit b74c3c7

File tree

3 files changed

+84
-18
lines changed

3 files changed

+84
-18
lines changed

‎example.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
require './src/Curl.php';
33
require './src/CurlException.php';
4+
require './src/CurlResponse.php';
45

56
use SujeetKumar\CurlLib\Curl;
67

‎src/Curl.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function sendRequest($method, $url, $data = array(), $options = array())
120120
public function getHeaders($url, $data = array(), $options = array()) {
121121
$headers = array();
122122
if ($res = $this->head($url, $data = array(), $options = array())) {
123-
$headers = $this->parseHeader($res);
123+
$headers = $res->getHeaders();
124124
}
125125
return $headers;
126126
}
@@ -311,7 +311,9 @@ public function execute($url = '') {
311311
return false;
312312
}
313313

314-
isset($this->options[CURLOPT_RETURNTRANSFER]) or $this->options[CURLOPT_RETURNTRANSFER] = true;
314+
$this->options[CURLOPT_HEADER] = true;
315+
$this->options[CURLOPT_RETURNTRANSFER] = true;
316+
315317
isset($this->options[CURLOPT_TIMEOUT]) or $this->options[CURLOPT_TIMEOUT] = $this->timeout;
316318
isset($this->options[CURLOPT_FAILONERROR]) or $this->options[CURLOPT_FAILONERROR] = $this->strict_mode;
317319

@@ -345,7 +347,13 @@ public function execute($url = '') {
345347
$response = $this->response;
346348
curl_close($this->request);
347349
$this->clear();
348-
return $response;
350+
351+
$header_size = $this->info['header_size'];
352+
$http_code = $info['http_code'];
353+
$headers = substr($response, 0, $header_size);
354+
$body = substr($response, $header_size);
355+
356+
return new CurlResponse($response, $headers, $body, $http_code);
349357
}
350358
} else {
351359
$this->clear();
@@ -397,21 +405,6 @@ public function errorMsg() {
397405
return $this->error_msg;
398406
}
399407

400-
/**
401-
* Parse raw header text
402-
* @param string $raw_header
403-
*/
404-
public function parseHeader($raw_header) {
405-
$headers = array();
406-
foreach (explode("\r\n", trim($raw_header, "\r\n")) as $line) {
407-
if (strpos($line, ':') !== false) {
408-
list($key, $value) = explode(':', $line, 2);
409-
$headers[trim($key)] = trim($value);
410-
}
411-
}
412-
return $headers;
413-
}
414-
415408
protected function initialize($config) {
416409
if (is_array($config)) {
417410
isset($config['timeout']) and $this->timeout = $config['timeout'];

‎src/CurlResponse.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
namespace SujeetKumar\CurlLib;
3+
4+
/**
5+
* CurlResponse class
6+
*
7+
* @author Sujeet <sujeetkv90@gmail.com>
8+
* @link https://github.com/sujeet-kumar/php-curl-lib
9+
*/
10+
class CurlResponse
11+
{
12+
public $response;
13+
public $headers;
14+
public $body;
15+
public $code;
16+
17+
public function __construct($response, $headers, $body, $code) {
18+
$this->response = $response;
19+
$this->headers = self::parseHeaders($headers);
20+
$this->body = $body;
21+
$this->code = $code;
22+
}
23+
24+
/**
25+
* Get response
26+
*/
27+
public function getResponse() {
28+
return $this->response;
29+
}
30+
31+
/**
32+
* Get response headers
33+
*/
34+
public function getHeaders() {
35+
return $this->headers;
36+
}
37+
38+
/**
39+
* Get response body
40+
*/
41+
public function getBody() {
42+
return $this->body;
43+
}
44+
45+
/**
46+
* Get response code
47+
*/
48+
public function getCode() {
49+
return $this->code;
50+
}
51+
52+
/**
53+
* Parse raw header text
54+
* @param string $raw_headers
55+
*/
56+
public static function parseHeaders($raw_headers) {
57+
is_array($raw_headers) or $raw_headers = explode("\r\n", trim($raw_headers, "\r\n"));
58+
$headers = array();
59+
foreach ($raw_headers as $raw_header) {
60+
if (strpos($raw_header, ':') !== false) {
61+
list($key, $value) = array_map('trim', explode(':', $raw_header, 2));
62+
// as per HTTP RFC Sec 4.2 combine same type of headers
63+
$headers[$key] = isset($headers[$key]) ? $headers[$key] . ',' . $value : $value;
64+
}
65+
}
66+
return $headers;
67+
}
68+
69+
public function __toString() {
70+
return $this->body;
71+
}
72+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /