Example #1
0
 /**
  * Return the request URL.
  *
  * @return string
  */
 public function getURL()
 {
     $url = $this->_internal_request->get_normalized_http_url();
     if ($this->getMethod() == KHttpRequest::GET) {
         $url = $this->_internal_request->to_url();
     }
     return $url;
 }
Example #2
0
 public function getAuthorizeURL($key = NULL, $secret = NULL, $callback = NULL)
 {
     $parameters = array();
     $parameters["oauth_token"] = $key;
     if ($callback) {
         $parameters["oauth_callback"] = $callback;
         $oauthRequest = new OAuthRequest('GET', self::AUTHORIZATION_URL, $parameters);
     } else {
         $oauthRequest = new OAuthRequest('GET', self::AUTHORIZATION_URL, $parameters);
     }
     return $oauthRequest->to_url();
 }
 /**
  *
  * @param OAuthRequest $Request 
  */
 protected function _Curl($Request)
 {
     $C = curl_init();
     curl_setopt($C, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($C, CURLOPT_SSL_VERIFYPEER, FALSE);
     switch ($Request->get_normalized_http_method()) {
         case 'POST':
             curl_setopt($C, CURLOPT_URL, $Request->get_normalized_http_url());
             curl_setopt($C, CURLOPT_POST, TRUE);
             curl_setopt($C, CURLOPT_POSTFIELDS, $Request->to_postdata());
             break;
         default:
             curl_setopt($C, CURLOPT_URL, $Request->to_url());
     }
     return $C;
 }
 /**
  *
  *
  * @param OAuthRequest $Request
  */
 protected function _curl($Request, $Post = null)
 {
     $C = curl_init();
     curl_setopt($C, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($C, CURLOPT_SSL_VERIFYPEER, false);
     switch ($Request->get_normalized_http_method()) {
         case 'POST':
             //            echo $Request->get_normalized_http_url();
             //            echo "\n\n";
             //            echo $Request->to_postdata();
             curl_setopt($C, CURLOPT_URL, $Request->get_normalized_http_url());
             //            curl_setopt($C, CURLOPT_HTTPHEADER, array('Authorization' => $Request->to_header()));
             curl_setopt($C, CURLOPT_POST, true);
             curl_setopt($C, CURLOPT_POSTFIELDS, $Request->to_postdata());
             break;
         default:
             curl_setopt($C, CURLOPT_URL, $Request->to_url());
     }
     return $C;
 }
 /**
  * Format and sign an OAuth / API request
  */
 function oAuthRequest2($url, $method, $parameters)
 {
     if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
         $url = $this->getFormat($url);
     }
     $defaults = array();
     $token = $this->token;
     $defaults['access_token'] = $token->key;
     $parameters = array_merge($defaults, $parameters);
     $request = new OAuthRequest($method, $url, $parameters);
     switch ($method) {
         case 'GET':
             return $this->http($request->to_url(), 'GET');
         default:
             return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
     }
 }
Example #6
0
 /**
  * HTTP通信を行う。
  * 
  * @param OAuthRequest $request リクエストオブジェクト
  * @param array $body_params POSTのBODYに指定するパラメータ
  * @return HttpResponse
  */
 private static function http($request, $body_params = array())
 {
     // cURLリソースの生成
     $ch = curl_init();
     // Locationヘッダは無視
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
     // サーバ証明書の検証を行わない
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     // レスポンスを文字列として取得する設定
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     // (出力結果に)ヘッダを含める
     curl_setopt($ch, CURLOPT_HEADER, true);
     if (strcasecmp($request->get_normalized_http_method(), 'POST') == 0) {
         // POST通信
         curl_setopt($ch, CURLOPT_POST, true);
         // URLを指定
         curl_setopt($ch, CURLOPT_URL, $request->get_normalized_http_url());
         // リクエストヘッダを設定
         curl_setopt($ch, CURLOPT_HTTPHEADER, array($request->to_header()));
         // リクエストパラメータを設定
         curl_setopt($ch, CURLOPT_POSTFIELDS, $body_params);
     } else {
         // URLを指定
         curl_setopt($ch, CURLOPT_URL, $request->to_url());
     }
     // 実行
     $result = curl_exec($ch);
     // HTTPステータスコードを取得
     $http_status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     // close curl resource to free up system resources
     curl_close($ch);
     return explode("\r\n\r\n", $result, 2);
 }
Example #7
0
 /**
  * do a request
  *
  * @param OAuthRequest $request
  * @return OAuthToken|null
  */
 private static function doRequest($request)
 {
     if ($request->get_normalized_http_method() == 'POST') {
         $data = self::doPost($request->get_normalized_http_url(), $request->to_postdata());
     } else {
         $data = self::doGet($request->to_url());
     }
     parse_str($data);
     if (isset($oauth_token) && isset($oauth_token_secret)) {
         return new ExtendedOAuthToken($oauth_token, $oauth_token_secret, $data);
     }
     return null;
 }
 /**
  * Sign the request using OAuth. This uses the consumer token and key
  * but 2 legged oauth doesn't require an access token and key. In situations where you want to
  * do a 'reverse phone home' (aka: gadget does a makeRequest to your server
  * and your server wants to retrieve more social information) this is the prefered
  * method.
  *
  * @param string $method the method (get/put/delete/post)
  * @param string $url the url to sign (http://site/social/rest/people/1/@me)
  * @param array $params the params that should be appended to the url (count=20 fields=foo, etc)
  * @param string $postBody for POST/PUT requests, the postBody is included in the signature
  * @return string the signed url
  */
 public function sign($method, $url, $params = array(), $postBody = false, &$headers = array())
 {
     $oauthRequest = new OAuthRequest($method, $url, $params);
     $params = $this->mergeParameters($params);
     foreach ($params as $key => $val) {
         if (is_array($val)) {
             $val = implode(',', $val);
         }
         $oauthRequest->set_parameter($key, $val);
     }
     if ($postBody && strlen($postBody)) {
         if ($this->useBodyHash) {
             $bodyHash = base64_encode(sha1($postBody, true));
             $oauthRequest->set_parameter("oauth_body_hash", $bodyHash);
         }
         if ($this->useBodyHack) {
             $oauthRequest->set_parameter($postBody, '');
         }
     }
     $oauthRequest->sign_request($this->signatureMethod, $this->consumerToken, $this->accessToken);
     if ($postBody && $this->useBodyHack) {
         unset($oauthRequest->parameters[$postBody]);
     }
     $signedUrl = $oauthRequest->to_url();
     return $signedUrl;
 }
Example #9
0
 /**
  * Call Mendeley API
  *
  * You should cache frequent calls to this method in your application. At least GET calls.
  *
  * @param string $method
  * @param string $url
  * @param array $params
  * @param boolean $authenticate
  */
 private function http($method, $url, $params = array(), $authentication = true)
 {
     if (!is_array($params)) {
         throw new Exception('HTTP params need to be array in Mendeley::http');
     }
     if ($authentication) {
         $url = self::MENDELEY_OAPI_PRIVATE_URL . $url;
         $token = $this->getAccessToken($this->signatureMethod, $this->consumer);
         $request = OAuthRequest::from_consumer_and_token($this->consumer, $token, $method, $url, $params);
         $request->sign_request($this->signatureMethod, $this->consumer, $token);
     } else {
         $url = self::MENDELEY_OAPI_PUBLIC_URL . $url;
         $params['consumer_key'] = $this->consumer->key;
         $request = new OAuthRequest($method, $url, $params);
     }
     if ($method === 'GET') {
         $url = $request->to_url();
     } else {
         $url = $request->get_normalized_http_url();
         $params = $request->to_postdata();
     }
     if ($request = MendeleyUtil::runCurl($url, $method, array(), $params)) {
         $request = json_decode($request);
     }
     return $request;
 }