summaryrefslogtreecommitdiffstats
path: root/src/PurpleCode
diff options
context:
space:
mode:
authorunknown <dmarkiew@CNU3429M9W.nsn-intra.net>2014-07-15 08:02:55 +0200
committerunknown <dmarkiew@CNU3429M9W.nsn-intra.net>2014-07-15 08:02:55 +0200
commit1884e50d534f9ca82527d06861cb7064fe1d9b40 (patch)
treed75cef59622d90249938b1aeaddf7202f6f43f0d /src/PurpleCode
parent0e0aa739ec43eb6146bd483b0edd12b5928d93ee (diff)
downloadphp.curl-1884e50d534f9ca82527d06861cb7064fe1d9b40.zip
php.curl-1884e50d534f9ca82527d06861cb7064fe1d9b40.tar.gz
php.curl-1884e50d534f9ca82527d06861cb7064fe1d9b40.tar.bz2
Get response header and allow to posprocess body with this information
Diffstat (limited to 'src/PurpleCode')
-rw-r--r--src/PurpleCode/PCurl/PCurl.php23
-rw-r--r--src/PurpleCode/PCurl/PJsonCurl.php18
-rw-r--r--src/PurpleCode/PCurl/PObjectCurl.php7
3 files changed, 41 insertions, 7 deletions
diff --git a/src/PurpleCode/PCurl/PCurl.php b/src/PurpleCode/PCurl/PCurl.php
index 4c2a428..9af612c 100644
--- a/src/PurpleCode/PCurl/PCurl.php
+++ b/src/PurpleCode/PCurl/PCurl.php
@@ -38,8 +38,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 +103,16 @@ 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);
+
+ $processedResponse = $this->postProcessResponseBody($header, $body);
+
+ return $body;
}
/**
@@ -137,6 +145,15 @@ class PCurl {
}
/**
+ * Allow to postprocess response body, ex. parse to specified format.
+ * @param string $header
+ * @param string $body
+ */
+ protected function postProcessResponseBody($header, $body){
+ return $body;
+ }
+
+ /**
* @return PCurl
*/
public function headers(array $headers) {
diff --git a/src/PurpleCode/PCurl/PJsonCurl.php b/src/PurpleCode/PCurl/PJsonCurl.php
index e5dcf26..30776a0 100644
--- a/src/PurpleCode/PCurl/PJsonCurl.php
+++ b/src/PurpleCode/PCurl/PJsonCurl.php
@@ -14,22 +14,36 @@ namespace PurpleCode\PCurl;
class PJsonCurl extends PCurl {
private $arrayResponse;
+ private $alwaysParseResponse;
public function __construct($host) {
parent::__construct($host);
$this->arrayResponse = false;
+ $this->alwaysParseResponse = true;
$this->contentTypeJson();
}
public function call($method, $url, $payload = '') {
$payload = json_encode($payload);
- $response = parent::call($method, $url, $payload);
- return json_decode($response, $this->arrayResponse);
+ return parent::call($method, $url, $payload);
+ }
+
+ protected function postProcessResponseBody($body, $header) {
+ $body = parent::postProcessResponseBody($body, $header);
+ if ($this->alwaysParseResponse || strpos($header,'application/json')!==false) {
+ return json_decode($body, $this->arrayResponse);
+ }
+
+ return $body;
}
public function arrayResponse($arrayResponse = true) {
$this->arrayResponse = $arrayResponse;
}
+ public function alwaysParseResponse($alwaysParseResponse = true) {
+ $this->alwaysParseResponse = $alwaysParseResponse;
+ }
+
}
diff --git a/src/PurpleCode/PCurl/PObjectCurl.php b/src/PurpleCode/PCurl/PObjectCurl.php
index 3a2f09c..238bf11 100644
--- a/src/PurpleCode/PCurl/PObjectCurl.php
+++ b/src/PurpleCode/PCurl/PObjectCurl.php
@@ -35,8 +35,11 @@ class PObjectCurl extends PCurl {
public function call($method, $url, $payload = '') {
$payload = $this->serialize($payload);
- $response = parent::call($method, $url, $payload);
- return $this->deserialize($response);
+ return parent::call($method, $url, $payload);
+ }
+
+ protected function postProcessResponseBody($body, $header) {
+ return $this->deserialize($body);
}
private function serialize($payload) {