Пример #1
0
 function testCustomMimeRegistering()
 {
     // Register new mime type handler for "application/vnd.nategood.message+xml"
     Httpful::register(self::SAMPLE_VENDOR_TYPE, new DemoMimeHandler());
     $this->assertTrue(Httpful::hasParserRegistered(self::SAMPLE_VENDOR_TYPE));
     $request = Request::init();
     $response = new Response('<xml><name>Nathan</name></xml>', self::SAMPLE_VENDOR_HEADER, $request);
     $this->assertEquals(self::SAMPLE_VENDOR_TYPE, $response->content_type);
     $this->assertEquals('custom parse', $response->body);
 }
Пример #2
0
 /**
  * Parse the response into a clean data structure
  * (most often an associative array) based on the expected
  * Mime type.
  *
  * @param string $body Http response body
  *
  * @return mixed the response parse accordingly
  */
 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);
 }