/** * Execute client WS request with token authentication * @param string $functionname * @param array $params * @param bool $json * @return mixed */ public function call($functionname, $params, $json = false) { if ($this->type == 'oauth') { $url = $this->serverurl . '?wsfunction=' . $functionname; $body = ''; $options = array(); if ($json) { $url .= '&alt=json'; $body = json_encode($params); } else { $body = format_postdata_for_curlcall($params); } // setup the client side OAuth $oauth_options = array('consumer_key' => $this->consumer->consumer_key, 'consumer_secret' => $this->consumer->consumer_secret, 'server_uri' => 'http://example.com/webservice/rest/server.php', 'request_token_uri' => 'http://example.com/maharadev/webservice/oauthv1.php/request_token', 'authorize_uri' => 'http://example.com/webservice/oauthv1.php/authorize', 'access_token_uri' => 'http://example.com/webservice/oauthv1.php/access_token'); $store = OAuthStore::instance("Session", $oauth_options, true); $store->addServerToken($this->consumer->consumer_key, 'access', $this->token['token'], $this->token['token_secret'], 1); $request = new OAuthRequester($url, 'POST', $options, $body); $result = $request->doRequest(0); if ($result['code'] != 200) { throw new Exception('REST OAuth error: ' . var_export($result, true)); } $result = $result['body']; if ($json) { $values = (array) json_decode($result, true); return $values; } } else { // do a JSON based call - just soooo easy compared to XML/SOAP if ($json) { $data = json_encode($params); $url = $this->serverurl . '?' . $this->auth . '&wsfunction=' . $functionname . '&alt=json'; $result = file_get_contents($url, false, stream_context_create(array('http' => array('method' => 'POST', 'header' => "Content-Type: application/json\r\nConnection: close\r\nContent-Length: " . strlen($data) . "\r\n", 'content' => $data)))); $values = (array) json_decode($result, true); return $values; } // default to parsing HTTP parameters $result = webservice_download_file_content($this->serverurl . '?' . $this->auth . '&wsfunction=' . $functionname, null, $params); } //after the call, for those not using JSON, parseout the results // from REST XML response to PHP $xml2array = new webservice_xml2array($result); $raw = $xml2array->getResult(); if (isset($raw['EXCEPTION'])) { $debug = isset($raw['EXCEPTION']['DEBUGINFO']) ? $raw['EXCEPTION']['DEBUGINFO']['#text'] : ''; throw new Exception('REST error: ' . $raw['EXCEPTION']['MESSAGE']['#text'] . ' (' . $raw['EXCEPTION']['@class'] . ') ' . $debug); } $result = array(); if (isset($raw['RESPONSE'])) { $node = $raw['RESPONSE']; if (isset($node['MULTIPLE'])) { $result = self::recurse_structure($node['MULTIPLE']); } else { if (isset($raw['RESPONSE']['SINGLE'])) { $result = $raw['RESPONSE']['SINGLE']; } else { // empty result ? $result = $raw['RESPONSE']; } } } return $result; }
/** * Execute test client WS request * @param string $serverurl * @param string $function * @param array $params * @return mixed webservice call return values */ public function simpletest($serverurl, $function, $params) { return webservice_download_file_content($serverurl . '&wsfunction=' . $function, null, $params); }