endPoint = $endPoint; $this->amfRequest = new SabreAMF_Message(); $this->amfOutputStream = new SabreAMF_OutputStream(); } /** * sendRequest * * sendRequest sends the request to the server. It expects the servicepath and methodname, and the parameters of the methodcall * * @param string $servicePath The servicepath (e.g.: myservice.mymethod) * @param array $data The parameters you want to send * @return mixed */ public function sendRequest($servicePath,$data) { // We're using the FLEX Messaging framework if($this->encoding & SabreAMF_Const::FLEXMSG) { // Setting up the message $message = new SabreAMF_AMF3_RemotingMessage(); $message->body = $data; // We need to split serviceName.methodName into separate variables $service = explode('.',$servicePath); $method = array_pop($service); $service = implode('.',$service); $message->operation = $method; $message->source = $service; $data = $message; } $this->amfRequest->addBody(array( // If we're using the flex messaging framework, target is specified as the string 'null' 'target' => $this->encoding & SabreAMF_Const::FLEXMSG?'null':$servicePath, 'response' => '/1', 'data' => $data )); $this->amfRequest->serialize($this->amfOutputStream); // The curl request $ch = curl_init($this->endPoint); curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_TIMEOUT,20); curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: ' . SabreAMF_Const::MIMETYPE)); curl_setopt($ch,CURLOPT_POSTFIELDS,$this->amfOutputStream->getRawData()); if ($this->httpProxy) { curl_setopt($ch,CURLOPT_PROXY,$this->httpProxy); } $result = curl_exec($ch); if (curl_errno($ch)) { throw new Exception('CURL error: ' . curl_error($ch)); false; } else { curl_close($ch); } $this->amfInputStream = new SabreAMF_InputStream($result); $this->amfResponse = new SabreAMF_Message(); $this->amfResponse->deserialize($this->amfInputStream); $this->parseHeaders(); foreach($this->amfResponse->getBodies() as $body) { if (strpos($body['target'],'/1')===0) return $body['data'] ; } } /** * addHeader * * Add a header to the client request * * @param string $name * @param bool $required * @param mixed $data * @return void */ public function addHeader($name,$required,$data) { $this->amfRequest->addHeader(array('name'=>$name,'required'=>$required==true,'data'=>$data)); } /** * setCredentials * * @param string $username * @param string $password * @return void */ public function setCredentials($username,$password) { $this->addHeader('Credentials',false,(object)array('userid'=>$username,'password'=>$password)); } /** * setHttpProxy * * @param mixed $httpProxy * @return void */ public function setHttpProxy($httpProxy) { $this->httpProxy = $httpProxy; } /** * parseHeaders * * @return void */ private function parseHeaders() { foreach($this->amfResponse->getHeaders() as $header) { switch($header['name']) { case 'ReplaceGatewayUrl' : if (is_string($header['data'])) { $this->endPoint = $header['data']; } break; } } } /** * Change the AMF encoding (0 or 3) * * @param int $encoding * @return void */ public function setEncoding($encoding) { $this->encoding = $encoding; $this->amfRequest->setEncoding($encoding & SabreAMF_Const::AMF3); } }