Example #1
0
 public function request(Url $url, $mode, $host, $name, $header = array())
 {
     if (empty($mode) || ($mode = self::getMode($mode)) === false) {
         throw new Exception('Invalid mode');
     }
     // discover service
     $url = $this->discover($url);
     // headers
     if ($this->oauth !== null) {
         $header = Request::mergeHeader(array('Accept' => 'application/json', 'Authorization' => $this->oauth->getAuthorizationHeader($url, $this->cred->getConsumerKey(), $this->cred->getConsumerSecret(), $this->cred->getToken(), $this->cred->getTokenSecret(), 'HMAC-SHA1', 'POST')), $header);
     } else {
         $header = Request::mergeHeader(array('Accept' => 'application/json'), $header);
     }
     // body
     $body = array('relation.ns' => self::NS, 'relation.mode' => $mode, 'relation.host' => $host, 'relation.name' => $name);
     $request = new PostRequest($url, $header, $body);
     $response = $this->http->request($request);
     if ($response->getCode() == 200) {
         $data = Json::decode($response->getBody());
         if (isset($data['success']) && $data['success'] === true) {
             return true;
         } else {
             $msg = isset($data['text']) ? $data['text'] : 'An error occured';
             throw new Exception($msg);
         }
     } else {
         throw new Exception('Invalid response code ' . $response->getCode());
     }
 }