diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/PurpleCode/PCurl/PCurl.php | 14 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PCurlJsonResponse.php | 32 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PCurlResponse.php | 39 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PJsonCurl.php | 4 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PObjectCurl.php | 3 |
5 files changed, 87 insertions, 5 deletions
diff --git a/src/PurpleCode/PCurl/PCurl.php b/src/PurpleCode/PCurl/PCurl.php index 4c2a428..4e9d4b6 100644 --- a/src/PurpleCode/PCurl/PCurl.php +++ b/src/PurpleCode/PCurl/PCurl.php @@ -12,8 +12,10 @@ namespace PurpleCode\PCurl;
require_once 'PCurlException.php';
+require_once 'PCurlResponse.php';
use PurpleCode\PCurl\PCurlException;
+use PurpleCode\PCurl\PCurlResponse;
class PCurl {
@@ -38,8 +40,8 @@ class PCurl { $this->setOption(CURLOPT_SSL_VERIFYPEER, true);
// should curl_exec return response, not print it on stdout
$this->setOption(CURLOPT_RETURNTRANSFER, true);
- // should not include headers in response
- $this->setOption(CURLOPT_HEADER, 0);
+ // should include headers in response
+ $this->setOption(CURLOPT_HEADER, 1);
}
/**
@@ -103,8 +105,14 @@ class PCurl { curl_close($curl);
throw new PCurlException($error);
}
+
+ $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
curl_close($curl);
- return $response;
+
+ $header = substr($response, 0, $header_size);
+ $body = substr($response, $header_size);
+
+ return new PCurlResponse($header, $body);
}
/**
diff --git a/src/PurpleCode/PCurl/PCurlJsonResponse.php b/src/PurpleCode/PCurl/PCurlJsonResponse.php new file mode 100644 index 0000000..df36ea4 --- /dev/null +++ b/src/PurpleCode/PCurl/PCurlJsonResponse.php @@ -0,0 +1,32 @@ +<?php + +/** + * PCurl is a REST client libary for PHP. + * + * See http://github.com/purplecode/php.curl for details. + * + * This code is licensed for use, modification, and distribution + * under the terms of the MIT License (see http://en.wikipedia.org/wiki/MIT_License) + */ + +namespace PurpleCode\PCurl; + +use PurpleCode\PCurl\PCurlResponse; + +class PCurlJsonResponse extends PCurlResponse { + private $parsedResponse; + + public function __construct($header, $body, $arrayResponse){ + if(! PCurlJsonResponse::isValidJson($body)) + throw new PCurlException("Invalid JSON response format"); + parent::__construct($header, json_decode($body, $arrayResponse)); + } + + /** + * Fast way to check if response body is in JSon format - see RFC4627, regexp part. + */ + private static function isValidJson($text){ + return !preg_match('/[^,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t]/', preg_replace('/"(\\.|[^"\\\\])*"/', '', $text)); + } + +} diff --git a/src/PurpleCode/PCurl/PCurlResponse.php b/src/PurpleCode/PCurl/PCurlResponse.php new file mode 100644 index 0000000..d616a95 --- /dev/null +++ b/src/PurpleCode/PCurl/PCurlResponse.php @@ -0,0 +1,39 @@ +<?php + +/** + * PCurl is a REST client libary for PHP. + * + * See http://github.com/purplecode/php.curl for details. + * + * This code is licensed for use, modification, and distribution + * under the terms of the MIT License (see http://en.wikipedia.org/wiki/MIT_License) + */ + +namespace PurpleCode\PCurl; + + +class PCurlResponse { + private $body; + private $header; + + public function __construct($header, $body){ + $this->header = $header; + $this->body = $body; + } + + public function setBody($body){ + $this->body = $body; + } + + public function getBody(){ + return $this->body; + } + + public function setHeader($header){ + $this->header = $header; + } + + public function getHeader(){ + return $this->header; + } +} diff --git a/src/PurpleCode/PCurl/PJsonCurl.php b/src/PurpleCode/PCurl/PJsonCurl.php index e5dcf26..aad8bd4 100644 --- a/src/PurpleCode/PCurl/PJsonCurl.php +++ b/src/PurpleCode/PCurl/PJsonCurl.php @@ -11,6 +11,8 @@ namespace PurpleCode\PCurl;
+use PurpleCode\PCurl\PCurlJsonResponse;
+
class PJsonCurl extends PCurl {
private $arrayResponse;
@@ -25,7 +27,7 @@ class PJsonCurl extends PCurl { public function call($method, $url, $payload = '') {
$payload = json_encode($payload);
$response = parent::call($method, $url, $payload);
- return json_decode($response, $this->arrayResponse);
+ return new PCurlJsonResponse($response->getHeader(), $response->getBody(), $this->arrayResponse);
}
public function arrayResponse($arrayResponse = true) {
diff --git a/src/PurpleCode/PCurl/PObjectCurl.php b/src/PurpleCode/PCurl/PObjectCurl.php index 3a2f09c..5fdca29 100644 --- a/src/PurpleCode/PCurl/PObjectCurl.php +++ b/src/PurpleCode/PCurl/PObjectCurl.php @@ -36,7 +36,8 @@ class PObjectCurl extends PCurl { public function call($method, $url, $payload = '') {
$payload = $this->serialize($payload);
$response = parent::call($method, $url, $payload);
- return $this->deserialize($response);
+ $response->setBody($this->deserialize($response));
+ return $response;
}
private function serialize($payload) {
|