Exemple #1
0
 /**
  * Decodes the response based on the content type
  *
  * @param \HttpSocketResponse $response
  * @return array $response
  * @author Dean Sofer
  */
 public function decode(\HttpSocketResponse $response)
 {
     // Get content type header
     $contentType = explode(';', $response->getHeader('Content-Type'));
     // Decode response according to content type
     switch ($contentType[0]) {
         case 'application/xml':
         case 'application/atom+xml':
         case 'application/rss+xml':
             App::uses('Xml', 'Utility');
             $Xml = Xml::build($response->body());
             $return = Xml::toArray($Xml);
             //one of the two lines of code following is unecessary.
             //Unset will delete the reference and mark the memory for garbage collection.
             //setting null will clear the memory immediately.
             //There is some overhead involved in shrinking the stack, so if you are calling this repeatedly it may not be faster.
             $Xml = null;
             unset($Xml);
             break;
         case 'application/json':
         case 'application/javascript':
         case 'text/javascript':
             $return = json_decode($response->body(), true);
             break;
         default:
             $return = $response->body();
             break;
     }
     return $return;
 }