/** * Register default mime handlers. Is idempotent. */ public static function registerHandlers() { if (self::$registered === true) { return; } // @todo check a conf file to load from that instead of // hardcoding into the library? $handlers = array(\Httpful\Mime::JSON => new \Httpful\Handlers\JsonHandler(), \Httpful\Mime::XML => new \Httpful\Handlers\XmlHandler(), \Httpful\Mime::FORM => new \Httpful\Handlers\FormHandler(), \Httpful\Mime::CSV => new \Httpful\Handlers\CsvHandler()); foreach ($handlers as $mime => $handler) { // Don't overwrite if the handler has already been registered if (Httpful::hasParserRegistered($mime)) { continue; } Httpful::register($mime, $handler); } self::$registered = true; }
/** * Parse the response into a clean data structure * (most often an associative array) based on the expected * Mime type. * @return array|string|object the response parse accordingly * @param string Http response body */ public function _parse($body) { // If the user decided to forgo the automatic // smart parsing, short circuit. if (!$this->request->auto_parse) { return $body; } // If provided, use custom parsing callback if (isset($this->request->parse_callback)) { return call_user_func($this->request->parse_callback, $body); } // Decide how to parse the body of the response in the following order // 1. If provided, use the mime type specifically set as part of the `Request` // 2. If a MimeHandler is registered for the content type, use it // 3. If provided, use the "parent type" of the mime type from the response // 4. Default to the content-type provided in the response $parse_with = $this->request->expected_type; if (empty($this->request->expected_type)) { $parse_with = Httpful::hasParserRegistered($this->content_type) ? $this->content_type : $this->parent_type; } return Httpful::get($parse_with)->parse($body); }