public function get($uriParams = []) { if (is_array($uriParams)) { $uriParams = '?' . http_build_query($uriParams); } elseif (!is_string($uriParams)) { throw new Exception('Unsupported variable type'); } $this->curl->setUrl($this->curl->getUrl() . $uriParams); return $this->curl->execute(); }
public function execute() { $result = parent::execute(); if ($result === FALSE) { return FALSE; } else { $header = substr($result, 0, $this->info['header_size']); $body = substr($result, $this->info['header_size']); $header = explode("\n", $header); $headers = array(); foreach ($header as $h) { $h = trim($h); if (empty($h)) { continue; } $hd = explode(':', $h); if (count($hd) > 1) { $headers[trim($hd[0])] = trim($hd[1]); } else { $headers[trim($hd[0])] = trim($hd[0]); } } $this->response_headers = $headers; $this->response_body = $body; if ($this->info['http_code'] == 200) { return $this->response_body; } else { return FALSE; } } }
function initialize() { if (!$this->fetched) { $config =& get_config(); $assetUrl = $config['asset_service'] . $this->node['AssetID']; $curl = new Curl(); $curl->create($assetUrl); $curl->option(CURLOPT_HEADER, true); $curl->option(CURLOPT_NOBODY, true); $response = $curl->execute(); if ($response) { $headers = http_parse_headers($response); $this->size = $headers['Content-Length']; $this->contentType = $headers['Content-Type']; if (isset($headers['ETag'])) { $this->etag = $headers['ETag']; } else { if (isset($headers['Etag'])) { $this->etag = $headers['Etag']; } else { $this->etag = ''; } } } else { log_message('error', "InventoryFile: Failed to fetch headers from {$assetUrl}"); } $this->fetched = true; } }
public function execute() { $curl_options = array(CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_URL => $this->generate_url()); $curl = new Curl($curl_options); $res = $curl->execute(); return json_decode($res); }
/** * @throws PrimePesaException * @throws \Exception */ public function execute() { $params = array(); $params['cmd'] = 'balance'; $params['secret_key'] = $this->getSecretKey(); $result = Curl::execute($params); $this->_assertNotError($result); $this->result = $result; }
/** * @return bool * @throws PrimePesaException * @throws \Exception */ public function isAuthentic() { $params = array(); $params['cmd'] = 'verify'; $params['secret_key'] = $this->getSecretKey(); $params = array_merge($params, $this->data); $results = Curl::execute($params); $this->_assertNotError($results); return $results['status'] == 'VERIFIED'; }
function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = '') { $ret = Curl::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password); if (false === $ret) { return false; } if (is_array($ret)) { return false; } return $ret; }
/** * url_exists function. * Checks to see if an url exists * * @access public * @param string $url * @return bool */ public static function url_exists($url) { $curl_options = array(CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_URL => $url); $curl = new Curl($curl_options); try { $curl->execute(); $error = FALSE; } catch (Exception $e) { $error = TRUE; } return $error; }
function create_asset($assetID, $creatorID, $contentType, $filename) { $CI =& get_instance(); $CI->load->library('Curl'); $CI->load->helper('path'); $filename = rtrim(set_realpath($filename), '/'); $params = array('AssetID' => $assetID, 'CreatorID' => $creatorID, 'Asset' => "@{$filename};type={$contentType}"); echo 'Posting ' . $filename . ' to ' . $CI->config->item('asset_service') . '<br/>'; $curl = new Curl(); $curl->create($CI->config->item('asset_service')); $curl->option(CURLOPT_POST, TRUE); $curl->option(CURLOPT_POSTFIELDS, $params); $curl->http_method('post'); $response = json_decode($curl->execute(), TRUE); if (!isset($response)) { $response = array('Message' => 'Invalid or missing response. ' . $curl->error_string); } return $response; }
public static function ping() { $url = "http://" . $_SERVER['HTTP_HOST'] . '/admin/ping/technorati'; $options = array(CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_URL => $url); $curl = new Curl($options); $curl->execute(); }
public function _zest_news() { return ""; $html = "<h2>Zest News</h2><hr/>"; $url = "http://zestcms.com/rss/4"; $curl_options = array(CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_URL => $url); $curl = new Curl($curl_options); $data = new SimpleXMLElement($curl->execute()); $data = $data->channel->item; $arr = array(); $i = 0; foreach ($data as $item) { if ($i == 5) { break; } $arr[] = $item; $i++; } $arr = array_reverse($arr); foreach ($arr as $item) { $html .= "<p>" . date('d M', strtotime($item->pubDate)) . "<br/><a href='" . $item->link . "' target='_BLANK'>" . $item->title . "</a></p><hr/>"; } return $html; }
/** * @param string $url * @param Curl $curl * @param callable $callback * @param int $maxAge * @param bool $refreshInBackground * @return mixed */ public static function requestCache($url, Curl $curl = null, $callback = null, $maxAge = self::DEFAULT_CACHE_MAX_AGE, $refreshInBackground = true) { $return = false; $returnValue = null; if (!$curl) { $curl = new Curl(); $return = true; $callback = function ($content) use(&$returnValue) { $returnValue = $content; }; } $stmt = self::getStatement('SELECT * FROM request_cache WHERE url = ?'); $stmt->execute(array($url)); $stmt->bindColumn('timestamp', $timestamp); $stmt->bindColumn('etag', $etag); $stmt->bindColumn('content', $content); $stmt->bindColumn('refresh', $refresh); $stmt->fetch(PDO::FETCH_BOUND); $shouldRefresh = $timestamp < time() - 60 * $maxAge; $refreshInBackground = $refreshInBackground && !is_null($content); if ($shouldRefresh && $refreshInBackground && $refresh < time() - 60) { self::getStatement('UPDATE request_cache SET refresh = ? WHERE url = ?')->execute(array(time(), $url)); self::$refreshUrls[] = $url; } if (!$shouldRefresh || $refreshInBackground) { $content = json_decode($content); $stmt = self::getStatement('SELECT url, content FROM request_cache WHERE parent = ? ORDER BY `timestamp` DESC'); while ($stmt->execute(array($url)) && ($data = $stmt->fetchObject())) { $content = array_merge($content, json_decode($data->content)); $url = $data->url; } if (is_callable($callback)) { $callback($content); } return $returnValue; } $responses = array(); $handleResponse = function (CurlResponse $response, $content, $parent = null) use(&$handleResponse, $curl, &$responses, $stmt, $callback) { $url = $response->request->url; if ($response && in_array($response->status, array(200, 304))) { $checkNext = false; if (304 == $response->status) { $response->content = $content; $checkNext = true; } elseif (false === stripos($response->contentType, 'json')) { $response->content = json_encode($response->content); } $responses[] = $response->content; Workflow::getStatement('REPLACE INTO request_cache VALUES(?, ?, ?, ?, 0, ?)')->execute(array($url, time(), $response->etag, $response->content, $parent)); if ($checkNext || $response->link && preg_match('/<(.+)>; rel="next"/U', $response->link, $match)) { $stmt = Workflow::getStatement('SELECT * FROM request_cache WHERE parent = ?'); $stmt->execute(array($url)); if ($checkNext) { $stmt->bindColumn('url', $nextUrl); } else { $nextUrl = $match[1]; } $stmt->bindColumn('etag', $etag); $stmt->bindColumn('content', $content); $stmt->fetch(PDO::FETCH_BOUND); if ($nextUrl) { $curl->add(new CurlRequest($nextUrl, $etag, function (CurlResponse $response) use($handleResponse, $url, $content) { $handleResponse($response, $content, $url); })); return; } } else { Workflow::getStatement('DELETE FROM request_cache WHERE parent = ?')->execute(array($url)); } } else { Workflow::getStatement('DELETE FROM request_cache WHERE url = ?')->execute(array($url)); $url = null; } if (is_callable($callback)) { if (empty($responses)) { $callback(array()); return; } if (1 === count($responses)) { $callback(json_decode($responses[0])); return; } $callback(array_reduce($responses, function ($content, $response) { return array_merge($content, json_decode($response)); }, array())); } }; $curl->add(new CurlRequest($url, $etag, function (CurlResponse $response) use(&$responses, $handleResponse, $callback, $content) { $handleResponse($response, $content); })); if ($return) { $curl->execute(); } return $returnValue; }
/** * cURL connection function. Returns data from Twitter and interfaces with * debug function. * * @param bool $login Login to Twitter? * @param bool $post Use POST instead of GET? * @param array $post_data Array data for POST. * @return string */ private function connect($login = FALSE, $post = FALSE, $post_data = NULL) { // If credentials are required add them if ($login) { $login = $this->login; } // add default header info $this->headers[] = 'Expect:'; $curl_options = array(CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_URL => $this->url); // curl init $curl = new Curl($curl_options); // add curl options if (count($this->headers) > 0) { $curl->addOption(CURLOPT_HTTPHEADER, $this->headers); } if ($login) { $curl->addOption(CURLOPT_USERPWD, $this->login); } if ($post) { $curl->addOption(CURLOPT_POST, TRUE); } if (is_array($post_data)) { $curl->addOption(CURLOPT_POSTFIELDS, $post_data); } // retrieve data $data = $curl->execute($this->ignore_curl_error_numbers); // set curl http status $this->status = $curl->status(); // set last call $this->last = $this->url; // clear settings $this->url = ''; $this->headers = ''; // debug output // if (!IN_PRODUCTION AND Kohana::config('twitter.debug')) $this->debugo($data, $post_data); return $data; }
/** * check_for_feed function. * * @access public * @param string $url * @return boolean */ public function check_for_feed($url) { $query = "select title,href from html where url='" . $url . "' and xpath='//link' and type='application/rss+xml'"; $q = urlencode($query); $url = 'http://query.yahooapis.com/v1/public/yql?q=' . $q . '&format=json'; $curl_options = array(CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_URL => $url); $curl = new Curl($curl_options); $data = $curl->execute(); $data = json_decode($data); if (!isset($data->error) && isset($data->query->results)) { if (is_array($data->query->results->link)) { foreach ($data->query->results->link as $link) { if (isset($link->href) && strstr($link->href, 'http://') != FALSE) { $this->feeds[$link->title] = array("profile" => $url, "feed" => str_replace('&lang', '&lang', $link->href)); } } } else { if (isset($data->query->results->link->href) && strstr($data->query->results->link->href, 'http://') != FALSE) { $this->feeds[$data->query->results->link->title] = array("profile" => $url, "feed" => str_replace('&lang', '&lang', $data->query->results->link->href)); } } return TRUE; } else { return FALSE; } }
public function mulitGet($urls) { $curl = new Curl(); $curl->setUrls($urls); return $curl->execute(); }
$sth = $db->prepare($sql); if ($sth->execute(array(':ID' => $_GET['cap']))) { if ($sth->rowCount() > 0) { $obj = $sth->fetchObject(); $resourceURL = $obj->Resource; // TODO: Handle relative resource URLs if (stripos($_SERVER['REQUEST_METHOD'], 'POST') !== FALSE) { // FIXME: Implement POST capability routing $curl = new Curl(); $curl->create($resourceURL); } else { if (stripos($_SERVER['REQUEST_METHOD'], 'GET') !== FALSE) { // FIXME: Properly proxy response codes $curl = new Curl(); $curl->create($resourceURL); echo $curl->execute(); exit; } else { log_message('warn', "Don't know how to route method " . $_SERVER['REQUEST_METHOD'] . " for capability {$resourceURL}"); header("HTTP/1.1 400 Bad Request"); echo 'Request method not understood'; exit; } } } else { log_message('warn', "Capability " . $_GET['cap'] . " not found"); header("HTTP/1.1 404 Not Found"); echo 'Capability not found'; exit; } } else {
function _getAccessToken() { //实例化数据库类并连接数据库 $wxConfig = M('wxConfig'); $accessToken = $wxConfig->where("_key='WX_ACCESS_TOKEN'")->getField('_value'); $access = $accessToken ? json_decode($access, true) : array(); if ($access['expires_in'] < time()) { //已经过期 $appid = $wxConfig->where("_key='WX_APPID'")->getField('_value'); $appsecret = $wxConfig->where("_key='WX_APPSECRET'")->getField('_value'); import("Curl"); $curl = new Curl(); $access = $curl->execute('get', 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret); $access_arr = json_decode($access, true); $access_arr['expires_in'] += time(); $data = array('_key' => 'WX_ACCESS_TOKEN', '_value' => json_encode($access_arr)); //保存 if (empty($accessToken)) { //需要创建 $wxConfig->add($data); } else { //需要更新 $wxConfig->where("_key='WX_ACCESS_TOKEN'")->save($data); } return $access_arr['access_token']; } else { return $access['access_token']; } }