public function action_do() { $params = Oauth2::parse_query(); try { if (empty($params['code']) or isset($params['error'])) { throw new Oauth2_Exception($params['error']); } $token = Remote::get($this->_configs['token_uri'], array(CURLOPT_POST => TRUE, CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded;charset=utf-8'), CURLOPT_POSTFIELDS => Oauth2::build_query(array('grant_type' => $this->_configs['grant_type'], 'code' => $params['code'], 'client_id' => $this->_configs['client_id'], 'redirect_uri' => $this->_configs['redirect_uri'], 'client_secret' => $this->_configs['client_secret'])))); $token = json_decode($token); if (isset($token->error)) { throw new Oauth2_Exception($token->error); } // Resource in json format $resource = Remote::get($this->_configs['access_uri'], array(CURLOPT_POST => TRUE, CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded;charset=utf-8'), CURLOPT_POSTFIELDS => Oauth2::build_query(array('oauth_token' => $token->access_token, 'timestamp' => $_SERVER['REQUEST_TIME'], 'refresh_token' => $token->refresh_token, 'expires_in' => $token->expires_in, 'client_id' => $this->_configs['client_id'])))); $this->request->response = $resource; } catch (Exception $e) { $error = $e->getMessage(); } if (isset($error)) { switch ($error) { case 'access_denied': $this->request->response = 'You have denied this request.'; break; default: $this->request->response = 'There must be some errors happen in this connection, please contact our web master.' . "[{$error}]"; break; } } }
public function action_test() { try { $resource = Remote::get('http://docs/api', array(CURLOPT_POST => TRUE, CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded;charset=UTF-8'), CURLOPT_POSTFIELDS => Oauth::build_query(array()))); } catch (Exception $e) { $resource = $e->getMessage(); } echo '<pre>' . print_r($resource, TRUE) . '</pre>'; }
public function action_get_xml($xml_uri = '') { $this->auto_render = false; // $this->request->headers['Content-Type'] = 'text/xml'; // header('Content-type: text/xml'); Request::instance()->headers = array('Content-type: text/xml'); Request::instance()->send_headers(); echo Remote::get(urldecode($_GET['id'])); // $data = file_get_contents(urldecode($_GET['id'])); // $this->request->response = $data; // print $data; }
/** * @return DOMDocument */ public function execute() { // Set the API access URL $url = ($this->sandbox ? gCheckout::URL_SANDBOX : gCheckout::URL_PRODUCTION) . $this->merchant_id; // Build the XML for the request $xml = $this->build(); // Get the server response $response = Remote::get($url, array(CURLOPT_USERPWD => $this->merchant_id . ':' . $this->merchant_key, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $xml)); // Create a new DOMDocument for the response $xml = new DOMDocument(); $xml->formatOutput = TRUE; $xml->loadXML($response); return $xml; }
public function action_tigia() { $this->auto_render = false; $eximbank = 'http://www.eximbank.com.vn/WebsiteExrate1/exchange.aspx'; $data = Remote::get($eximbank); $pattern = '/<table[ ]class="width_tb".*>(.*)<\\/table>.*/ismxU'; preg_match($pattern, $data, $matches); if (count($matches) > 0) { echo $matches[1]; } else { echo "Không có dữ liệu"; } //print_r($matches); //echo "<hr/>"; //echo $data; }
/** * Funkcja odpowiedzialna za pobranie informacji o filmie. * @param string $path * @return array */ public static function getMovie($path) { // Trzymamy ciasteczka (czasami wyskakują reklamy) Remote::$default_options += array(CURLOPT_COOKIEFILE => dirname(__FILE__) . DIRECTORY_SEPARATOR . Filmweb_Parser::COOKIES . DIRECTORY_SEPARATOR . 'filmweb', CURLOPT_COOKIEJAR => dirname(__FILE__) . DIRECTORY_SEPARATOR . Filmweb_Parser::COOKIES . DIRECTORY_SEPARATOR . 'filmweb'); // Wywowałmy filmweba.pl - dla usunięcia reklamy. Remote::get(Filmweb_Parser::URL); $request = Filmweb_Parser::URL; $response = Remote::get($request . $path); $data = new stdClass(); foreach (Filmweb_Parser::$regexps as $k => $r) { if ($r['all']) { preg_match_all($r['reg'], $response, $found); } else { preg_match($r['reg'], $response, $found); } if (isset($found[$r['data']])) { if ($r['all']) { if ($r['references']) { $a = array(); foreach ($found[$r['data']] as $key => $v) { $ref = $r['references'][0]::${$r}['references'][1]; $a[] = array('key' => $v, 'value' => $ref[$v]); } } $data->{$k} = $a; } else { if (isset($r['filter'])) { if (is_array($r['filter'])) { if (isset($r['filter'][2])) { $data->{$k} = call_user_func($r['filter'][0], $r['filter'][1], $r['filter'][2], $found[$r['data']]); } else { $data->{$k} = call_user_func($r['filter'][0], $r['filter'][1], $found[$r['data']]); } } else { $data->{$k} = call_user_func($r['filter'], $found[$r['data']]); } } else { $data->{$k} = $found[$r['data']]; } } } } return $data; }
/** * Download a file to a new location. If no filename is provided, * the original filename will be used, with a unique prefix added. * * @param string remote url * @param string new filename * @param string new directory * @param integer chmod mask * @return array on success, upload style file array * @return false on failure */ public static function download($url, $filename = NULL, $directory = NULL, $chmod = 0644) { if (!Valid::url($url)) { return false; } // If no filename given, use remote filename with uniqid $original_filename = basename(parse_url($url, PHP_URL_PATH)); if ($filename === null) { $filename = uniqid() . $original_filename; } // Remove spaces from the filename if (Upload::$remove_spaces === true) { $filename = preg_replace('/\\s+/', '_', $filename); } // Use the pre-configured upload directory if not given if ($directory === null) { $directory = Upload::$default_directory; } if (!is_dir($directory) || !is_writable(realpath($directory))) { throw new Kohana_Exception('Directory :dir must be writable', array(':dir' => Kohana::debug_path($directory))); } // Make the filename into a complete path $filename = realpath($directory) . DIRECTORY_SEPARATOR . $filename; // Download file try { // Dummy escape to get rid of spaces to awoid 400 Bad Request $url = str_replace(' ', '%20', $url); $fh = fopen($filename, 'w'); Remote::get($url, null, array(CURLOPT_RETURNTRANSFER => true, CURLOPT_FILE => $fh)); $size = Arr::get(fstat($fh), 'size', 0); fclose($fh); // Set permissions if ($chmod !== false) { chmod($filename, $chmod); } // Build file array $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $filename); finfo_close($finfo); return array('error' => UPLOAD_ERR_OK, 'name' => $original_filename, 'type' => $mime, 'tmp_name' => $filename, 'size' => $size); } catch (Kohana_Exception $e) { return false; } }
/** * Execute the request and return a response. * * @param string request type: GET, POST, etc (NULL for header) * @param array additional cURL options * @return string request response body * @uses OAuth_Request::check * @uses Arr::get * @uses Remote::get */ public function execute(array $options = NULL) { // Check that all required fields are set $this->check(); // Get the URL of the request $url = $this->url; if (!isset($options[CURLOPT_CONNECTTIMEOUT])) { // Use the request default timeout $options[CURLOPT_CONNECTTIMEOUT] = $this->timeout; } if ($this->send_header) { // Get the the current headers $headers = Arr::get($options, CURLOPT_HTTPHEADER, array()); // Add the Authorization header $headers[] = 'Authorization: ' . $this->as_header(); // Store the new headers $options[CURLOPT_HTTPHEADER] = $headers; } if ($this->method === 'POST') { // Send the request as a POST $options[CURLOPT_POST] = TRUE; if ($post = $this->as_query()) { // Attach the post fields to the request $options[CURLOPT_POSTFIELDS] = $post; } } elseif ($query = $this->as_query()) { // Append the parameters to the query string $url = "{$url}?{$query}"; } return Remote::get($url, $options); }
private function _get_status() { $data = array('pos_id' => Arr::get($_POST, 'pos_id'), 'session_id' => Arr::get($_POST, 'session_id'), 'ts' => time(), 'key' => $this->_pos_data['key']); $data['sig'] = $this->_get_sig($data); unset($data['key']); $url = self::PAYMENT_URL . $this->_codepage . '/' . self::PAYMENT_GET . '/' . $this->_format; $response = Remote::get($url, array(CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => http_build_query($data))); if ($this->_format == self::PAYMENT_FORMAT_XML) { $xml = new SimpleXMLElement($response); $pending = array(1, 4, 5); $success = array(99); $fail = array(2, 3, 7, 888); if (in_array((int) $xml->trans->status, $pending)) { return Payment::STATUS_PENDING; } if (in_array((int) $xml->trans->status, $success)) { return Payment::STATUS_SUCCESS; } if (in_array((int) $xml->trans->status, $fail)) { return Payment::STATUS_FAILED; } return FALSE; } }
public function api_connect($request_url, $method, array $post_data = array()) { $method = strtolower($method); if ($method != 'get' and $method != 'post') { throw new Exception("api_connect: method supplied must be either 'post' or 'get'"); } $num_requests_sent = 0; $response = ""; while (TRUE) { if ($num_requests_sent > $this->connection_retries) { print "Could not connect to API with request: {$request_url}\n"; // Add ERROR entry to gather log $this->model_gather->insert_gather_log(array('project_id' => $this->project_id, 'search_query' => $error, 'date' => time(), 'results_gathered' => 0, 'error' => 1)); $this->api_connect_error = 1; break; } else { if ($num_requests_sent > 0) { print "Re-trying ({$num_requests_sent})...\n"; } // Try connecting to API $error = ""; try { if ($method == "get") { $response = Remote::get($request_url, array(CURLOPT_RETURNTRANSFER => TRUE)); } else { if ($method == "post") { $response = Remote::get($request_url, array(CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => http_build_query($post_data))); } } } catch (Exception $e) { $error = $e->getMessage(); $num_requests_sent++; sleep($this->wait_before_retry); // Wait before trying to reconnect } if (!$error) { print "Successfully connected to API!\n"; break; } } } return $response; }
/** * Makes an API request and returns the response. * * @param string method to call * @param array query parameters * @return array */ protected function _request($method, array $params = NULL) { // Add the token to the parameters $params['token'] = $this->_token; try { // Make an API request $response = Remote::get(Todoist::API_URL . $method . '?' . http_build_query($params, NULL, '&')); } catch (Kohana_Exception $e) { throw new Todoist_Exception('API :method request failed, API may be offline', array(':method' => $method)); } // Return the decode response return json_decode($response, TRUE); }
private function generate_cluster_map($project_id, $chid, $chart_file) { $api_url = $this->chart_api_url . "?chid={$chid}"; // Open chart file and extract data $file_handle = fopen($chart_file, "r"); $chart_params = array(); while (!feof($file_handle)) { $line = rtrim(fgets($file_handle)); $param_ex = explode("=", $line); $param_name = $param_ex[0]; $param_vals = $param_ex[1]; if ($param_name == "mpids") { // List of cluster_ids in order displayed on chart $cluster_ids = explode(",", $param_vals); } else { if ($param_name == "mps") { // List of cluster sizes (number of documents) in order displayed on chart $cluster_sizes = explode(",", $param_vals); } else { // Parameter is chart param $chart_params[$param_name] = $param_vals; if ($param_name == "chd") { $chd_ex = explode("|", substr($param_vals, 2)); $cluster_scores = explode(",", $chd_ex[1]); } } } } fclose($file_handle); $chart_params['chof'] = 'json'; // tell API to return image map HTML // Send the POST request, parse json data, & compile image map HTML $response = Remote::get($api_url, array(CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => http_build_query($chart_params))); $image_map_html = ''; $json = json_decode($response, true); $num_results = count($json['chartshape']); if ($num_results > 0) { $i = 0; foreach ($json['chartshape'] as $map_item) { if ($map_item['type'] == "CIRCLE") { $coords_str = implode(",", $map_item['coords']); $title = $cluster_sizes[$i] . " documents (score: " . $cluster_scores[$i] . ")"; $href = "javascript:startLyteframe('" . $title . "', '" . Url::base() . "index.php/results/cluster_summary/{$project_id}?cluster_id=" . $cluster_ids[$i] . "')"; $image_map_html .= '<area name="' . $map_item['name'] . '" shape="' . $map_item['type'] . '" class="noborder icolorff0000" coords="' . $coords_str . '" href="' . $href . '" title="' . $title . '">'; $i++; } } } return $image_map_html; }