Ejemplo n.º 1
0
 /**
  * Sends a JSON-RPC request
  * @param Tivoka_Request $request A Tivoka request
  * @return Tivoka_Request if sent as a batch request the BatchRequest object will be returned
  */
 public function send($request)
 {
     if (func_num_args() > 1) {
         $request = func_get_args();
     }
     if (is_array($request)) {
         $request = Tivoka::createBatch($request);
     }
     if (!$request instanceof Tivoka_Request) {
         throw new Tivoka_Exception('Invalid data type to be sent to server');
     }
     $parse = parse_url($this->target);
     // preparing connection...
     $context = stream_context_create(array($parse['scheme'] => array('content' => (string) $request, 'header' => "Content-Type: application/json\r\n" . "Connection: Close\r\n", 'method' => 'POST', 'timeout' => 10.0)));
     //sending...
     if ($this->oauth_consumer) {
         $oauth = new OAuth_Request($this->oauth_consumer, 'POST', $this->target, array(), (string) $request);
         $response = $oauth->request($context);
     } else {
         $response = @file_get_contents($this->target, false, $context);
     }
     if ($response === FALSE) {
         throw new Tivoka_Exception('Connection to "' . $this->target . '" failed', Tivoka::ERR_CONNECTION_FAILED);
     }
     $request->setResponse($response);
     return $request;
 }