protected function getFormattedResponse($response) { if (array_key_exists($this->getAcceptType(), $this->autoDetectFormats)) { switch ($this->supportedFormats[$this->autoDetectFormats[$this->getAcceptType()]]) { case self::APPLICATION_JSON: $response = EActiveResourceParser::JSONtoArray($response); break; case self::APPLICATION_XML: $response = EActiveResourceParser::XMLToArray($response); break; default: throw new CException('Accept Type ' . $this->getAcceptType() . ' not implemented!'); } } return $response; }
/** * Creates a new request, sends and receives the data. * @param string $uri The uri this request is sent to. * @param string $method The method (GET,PUT,POST,DELETE) * @param array $data The data to be sent as array * @return array The parsed response as PHP array */ protected function sendRequest($uri, $method = EActiveResourceRequest::METHOD_GET, $data = null) { $request = new EActiveResourceRequest(); /* if($data) switch($this->getContentType()) { case EActiveResourceRequest::APPLICATION_JSON: $data= EActiveResourceParser::arrayToJSON($data); $headers=array( 'Content-Length: ' .strlen($data), 'Content-Type: ' .$this->getContentType(), 'Accept: ' .$this->getAcceptType(), ); break; case EActiveResourceRequest::APPLICATION_XML: $data= EActiveResourceParser::arrayToXML($data); $headers=array( 'Content-Length: ' .strlen($data), 'Content-Type: ' .$this->getContentType(), 'Accept: ' .$this->getAcceptType(), ); break; case EActiveResourceRequest::APPLICATION_FORM_URL_ENCODED: $data=EActiveResourceParser::arrayToFormData($data); $headers=array( 'Content-Length: ' .strlen($data), 'Content-Type: ' .$this->getContentType(), 'Accept: ' .$this->getAcceptType(), ); break; } else $headers=array( 'Accept: ' .$this->getAcceptType(), ); if($data) Yii::trace('Sending '.$method.' request to '.$uri.' with content-type:'.$this->getContentType().', accept: '.$this->getAcceptType().' and data: '.$data.' with '.$headers[0],'ext.EActiveResource'); else Yii::trace('Sending '.$method.' request to '.$uri.' without data, accepting: '.$this->getAcceptType(),'ext.EActiveResource'); */ $headers = array('X-mediav-auth:' . $this->getMetaData()->mediavAuth, 'X-mediav-entity:' . $this->getMetaData()->mediavEnity, 'Host:' . $this->getMetaData()->mediavHost); $response = $request->run($uri, $method, $data, $headers); Yii::trace('The service responded with: ' . $response, 'ext.EActiveResource'); //now build an array out of the response switch ($this->getAcceptType()) { case EActiveResourceRequest::APPLICATION_JSON: $parsedResponse = EActiveResourceParser::JSONToArray($response); return $parsedResponse; break; case EActiveResourceRequest::APPLICATION_XML: $parsedResponse = EActiveResourceParser::XMLToArray($response); return $parsedResponse; break; } }
/** * Parsed the data according to the content type to build a valid request (JSON or XML) * @return string the JSON or XML encoded string, null if no data is set */ public function getParsedData() { if (isset($this->_parsedData)) { return $this->_parsedData; } if (!is_null($this->getData())) { switch ($this->getContentType()) { case self::APPLICATION_JSON: $parsedData = EActiveResourceParser::arrayToJSON($this->getData()); break; case self::APPLICATION_XML: $parsedData = EActiveResourceParser::arrayToXML($this->getData()); break; default: throw new CException('Content Type ' . $this->getContentType() . ' not implemented!'); } return $this->_parsedData = $parsedData; } return null; }
/** * Internally used to parse the raw response from the service * and create an PHP array according to the accept type (JSON, XML) */ public function parseData() { Yii::trace("Response took " . $this->_info['total_time'] . " seconds:\n" . $this->getRawData(), 'ext.EActiveResource.response'); switch ($this->_acceptType) { case EActiveResourceRequest::APPLICATION_JSON: $this->_parsedData = EActiveResourceParser::JSONtoArray($this->getRawData()); break; case EActiveResourceRequest::APPLICATION_XML: $this->_parsedData = EActiveResourceParser::XMLtoArray($this->getRawData()); break; case null: break; default: throw new EActiveResourceException('Accept Type ' . $info['content_type'] . ' not implemented!'); } }