Skip to content
Browse files

turn off the Exception raised by an fopen() failure by default, and m…

…ake the rest of the code smarter about handling the situation where fopen() was unsuccessful.
  • Loading branch information...
1 parent 291b51a commit 5230845b04f26a8a895552e3b13129b22dd27ded @funkatron funkatron committed Apr 16, 2012
Showing with 64 additions and 14 deletions.
  1. +64 −14 Resty.php
View
78 Resty.php
@@ -9,7 +9,7 @@ class Resty
/**
* The version of this lib
*/
- const VERSION = '0.3.7';
+ const VERSION = '0.3.8';
const DEFAULT_TIMEOUT = 240;
@@ -76,6 +76,13 @@ class Resty
protected $silence_fopen_warning = true;
/**
+ * by default, don't raise an exception if fopen() fails
+ * @var boolean
+ */
+ protected $raise_fopen_exception = false;
+
+
+ /**
* content-types that will trigger JSON parsing of body
* @var array
*/
@@ -117,8 +124,11 @@ function __construct($opts=null) {
if (!empty($opts['onResponseLog']) && ($opts['onResponseLog'] instanceof Closure)) {
$this->callbacks['onResponseLog'] = $opts['onResponseLog'];
}
- if (!empty($opts['silence_fopen_warning']) && ($opts['silence_fopen_warning'] instanceof Closure)) {
- $this->silenceFopenWarning($opts['silence_fopen_warning']);
+ if (isset($opts['silence_fopen_warning'])) {
+ $this->silenceFopenWarning((bool)$opts['silence_fopen_warning']);
+ }
+ if (isset($opts['raise_fopen_exception'])) {
+ $this->raiseFopenException((bool)$opts['raise_fopen_exception']);
}
}
@@ -406,6 +416,18 @@ public function debug($state=null) {
}
/**
+ * raise an exception from fopen if trying to open stream fails
+ * @param boolean $state=null optional, set the state
+ * @return boolean the current state
+ */
+ public function raiseFopenException($state=null) {
+ if (isset($state)) {
+ $this->raise_fopen_exception = (bool)$state;
+ }
+ return $this->raise_fopen_exception;
+ }
+
+ /**
* silence warnings from fopen when trying to open stream
* @param boolean $state=null optional, set the state
* @return boolean the current state
@@ -509,10 +531,17 @@ protected function buildHeadersArray($headers) {
*/
protected function metaToHeaders($meta) {
$headers = array();
+
+ if (!isset($meta['wrapper_data'])) {
+ return $headers;
+ }
+
foreach ($meta['wrapper_data'] as $value) {
if (strpos($value, 'HTTP') !== 0) {
preg_match("|^([^:]+):\s?(.+)$|", $value, $matches);
- $headers[trim($matches[1])] = trim($matches[2], " \t\n\r\0\x0B\"");
+ if (is_array($matches) && isset($matches[2])) {
+ $headers[trim($matches[1])] = trim($matches[2], " \t\n\r\0\x0B\"");
+ }
}
}
return $headers;
@@ -525,8 +554,11 @@ protected function metaToHeaders($meta) {
*/
protected function getStatusCode($meta) {
$matches = array();
+ $status = 0;
preg_match("|\s(\d\d\d)\s?|", $meta['wrapper_data'][0], $matches);
- $status = (int)trim($matches[1]);
+ if (is_array($matches) && isset($matches[1])) {
+ $status = (int)trim($matches[1]);
+ }
return $status;
}
@@ -633,6 +665,8 @@ public function sendRequest($url, $method='GET', $querydata=null, $headers=null,
$resp['meta'] = $resp_data['meta'];
$resp['body'] = $resp_data['body'];
+ $resp['error'] = $resp_data['error'];
+ $resp['error_msg'] = $resp_data['error_msg'];
$resp['status'] = $this->getStatusCode($resp['meta']);
$resp['headers'] = $this->metaToHeaders($resp['meta']);
$this->log($resp);
@@ -660,7 +694,12 @@ public function sendRequest($url, $method='GET', $querydata=null, $headers=null,
*/
protected function makeStreamRequest($url, $opts) {
- $resp_data = array();
+ $resp_data = array(
+ 'meta' => null,
+ 'body' => null,
+ 'error' => true,
+ 'error_msg' => null,
+ );
$context = stream_context_create($opts);
@@ -675,21 +714,32 @@ protected function makeStreamRequest($url, $opts) {
}
if (!$stream) {
+
$req_time = static::calc_time_passed($start_time);
$opts_json = !empty($opts) ? json_encode($opts) : 'null';
$msg = "Stream open failed for '{$url}'; req_time: {$req_time}; opts: {$opts_json}";
$this->log($msg);
- throw new Exception($msg);
- }
- $this->log("Getting metadata…");
- $resp_data['meta'] = stream_get_meta_data($stream);
+ if ($this->raise_fopen_exception) {
+ throw new Exception($msg);
+ } else {
+ $resp_data['error'] = true;
+ $resp_data['error_msg'] = $msg;
+ }
- $this->log("Getting response…");
- $resp_data['body'] = stream_get_contents($stream);
+ } else {
+
+ $this->log("Getting metadata…");
+ $resp_data['meta'] = stream_get_meta_data($stream);
+
+ $this->log("Getting response…");
+ $resp_data['body'] = stream_get_contents($stream);
+
+ $this->log("Closing stream…");
+ fclose($stream);
+
+ }
- $this->log("Closing stream…");
- fclose($stream);
if ($this->debug) {
$req_time = static::calc_time_passed($start_time);

0 comments on commit 5230845

Please sign in to comment.
Something went wrong with that request. Please try again.