private function request($latitude, $longitude, $time = null, $options = array()) { $request_url = self::API_ENDPOINT . '[APIKEY]' . '/' . $latitude . ',' . $longitude . (is_null($time) ? '' : ',' . $time); if (!empty($options)) { $request_url .= '?' . http_build_query($options); } \Debug::Audit('Calling API with: ' . $request_url); $request_url = str_replace('[APIKEY]', $this->api_key, $request_url); $httpOptions = array(CURLOPT_TIMEOUT => 20, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_USERAGENT => 'Xibo Digital Signage', CURLOPT_HEADER => false, CURLINFO_HEADER_OUT => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => $request_url); // Proxy support if (\Config::GetSetting('PROXY_HOST') != '' && !\Config::isProxyException($request_url)) { $httpOptions[CURLOPT_PROXY] = \Config::GetSetting('PROXY_HOST'); $httpOptions[CURLOPT_PROXYPORT] = \Config::GetSetting('PROXY_PORT'); if (\Config::GetSetting('PROXY_AUTH') != '') { $httpOptions[CURLOPT_PROXYUSERPWD] = \Config::GetSetting('PROXY_AUTH'); } } $curl = curl_init(); curl_setopt_array($curl, $httpOptions); $result = curl_exec($curl); // Get the response headers $outHeaders = curl_getinfo($curl); if ($outHeaders['http_code'] == 0) { // Unable to connect \Debug::Error('Unable to reach Forecast API. No Host Found (HTTP Code 0). Curl Error = ' . curl_error($curl)); return false; } else { if ($outHeaders['http_code'] != 200) { \Debug::Error('ForecastIO API returned ' . $outHeaders['http_code'] . ' status. Unable to proceed. Headers = ' . var_export($outHeaders, true)); // See if we can parse the error. $body = json_decode($result); \Debug::Error('ForecastIO Error: ' . (isset($body->errors[0]) ? $body->errors[0]->message : 'Unknown Error')); return false; } } // Parse out header and body $body = json_decode($result); return $body; }
public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) { if (class_exists('idna_convert')) { $idn = new idna_convert(); $parsed = SimplePie_Misc::parse_url($url); $url = SimplePie_Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], $parsed['fragment']); } $this->url = $url; $this->useragent = $useragent; if (preg_match('/^http(s)?:\\/\\//i', $url)) { if ($useragent === null) { $useragent = ini_get('user_agent'); $this->useragent = $useragent; } if (!is_array($headers)) { $headers = array(); } if (!$force_fsockopen && function_exists('curl_exec')) { $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL; $fp = curl_init(); $headers2 = array(); foreach ($headers as $key => $value) { $headers2[] = "{$key}: {$value}"; } if (version_compare(SimplePie_Misc::get_curl_version(), '7.10.5', '>=')) { curl_setopt($fp, CURLOPT_ENCODING, ''); } curl_setopt($fp, CURLOPT_URL, $url); curl_setopt($fp, CURLOPT_HEADER, 1); curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1); curl_setopt($fp, CURLOPT_TIMEOUT, $timeout); curl_setopt($fp, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($fp, CURLOPT_REFERER, $url); curl_setopt($fp, CURLOPT_USERAGENT, $useragent); curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2); if (!ini_get('open_basedir') && !ini_get('safe_mode') && version_compare(SimplePie_Misc::get_curl_version(), '7.15.2', '>=')) { curl_setopt($fp, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($fp, CURLOPT_MAXREDIRS, $redirects); } // Dan Garner PATCH if (Config::GetSetting('PROXY_HOST') != '' && !Config::isProxyException($url)) { curl_setopt($fp, CURLOPT_PROXY, Config::GetSetting('PROXY_HOST')); curl_setopt($fp, CURLOPT_PROXYPORT, Config::GetSetting('PROXY_PORT')); if (Config::GetSetting('PROXY_AUTH') != '') { curl_setopt($fp, CURLOPT_PROXYUSERPWD, Config::GetSetting('PROXY_AUTH')); } } $this->headers = curl_exec($fp); if (curl_errno($fp) === 23 || curl_errno($fp) === 61) { curl_setopt($fp, CURLOPT_ENCODING, 'none'); $this->headers = curl_exec($fp); } if (curl_errno($fp)) { $this->error = 'cURL error ' . curl_errno($fp) . ': ' . curl_error($fp); $this->success = false; } else { $info = curl_getinfo($fp); curl_close($fp); // Remove headers from redirects $this->headers = explode("\r\n\r\n", $this->headers, $info['redirect_count'] + 1); $this->headers = array_pop($this->headers); // DG: Patch to strip double headers for HTTPS Proxies (they add headers without incrementing redirect count) $this->headers = SimplePie_HTTP_Parser::strip_double_headers($this->headers); //Debug::Audit('Headers: ' . var_export($this->headers, true)); $parser = new SimplePie_HTTP_Parser($this->headers); if ($parser->parse()) { $this->headers = $parser->headers; $this->body = $parser->body; $this->status_code = $parser->status_code; if ((in_array($this->status_code, array(300, 301, 302, 303, 307)) || $this->status_code > 307 && $this->status_code < 400) && isset($this->headers['location']) && $this->redirects < $redirects) { $this->redirects++; $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url); return $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen); } } } } else { $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_FSOCKOPEN; $url_parts = parse_url($url); $socket_host = $url_parts['host']; if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https') { $socket_host = "ssl://{$url_parts['host']}"; $url_parts['port'] = 443; } if (!isset($url_parts['port'])) { $url_parts['port'] = 80; } $fp = @fsockopen($socket_host, $url_parts['port'], $errno, $errstr, $timeout); if (!$fp) { $this->error = 'fsockopen error: ' . $errstr; $this->success = false; } else { stream_set_timeout($fp, $timeout); if (isset($url_parts['path'])) { if (isset($url_parts['query'])) { $get = "{$url_parts['path']}?{$url_parts['query']}"; } else { $get = $url_parts['path']; } } else { $get = '/'; } $out = "GET {$get} HTTP/1.1\r\n"; $out .= "Host: {$url_parts['host']}\r\n"; $out .= "User-Agent: {$useragent}\r\n"; if (extension_loaded('zlib')) { $out .= "Accept-Encoding: x-gzip,gzip,deflate\r\n"; } if (isset($url_parts['user']) && isset($url_parts['pass'])) { $out .= "Authorization: Basic " . base64_encode("{$url_parts['user']}:{$url_parts['pass']}") . "\r\n"; } foreach ($headers as $key => $value) { $out .= "{$key}: {$value}\r\n"; } $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); $info = stream_get_meta_data($fp); $this->headers = ''; while (!$info['eof'] && !$info['timed_out']) { $this->headers .= fread($fp, 1160); $info = stream_get_meta_data($fp); } if (!$info['timed_out']) { $parser = new SimplePie_HTTP_Parser($this->headers); if ($parser->parse()) { $this->headers = $parser->headers; $this->body = $parser->body; $this->status_code = $parser->status_code; if ((in_array($this->status_code, array(300, 301, 302, 303, 307)) || $this->status_code > 307 && $this->status_code < 400) && isset($this->headers['location']) && $this->redirects < $redirects) { $this->redirects++; $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url); return $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen); } if (isset($this->headers['content-encoding'])) { // Hey, we act dumb elsewhere, so let's do that here too switch (strtolower(trim($this->headers['content-encoding'], "\t\n\r "))) { case 'gzip': case 'x-gzip': $decoder = new SimplePie_gzdecode($this->body); if (!$decoder->parse()) { $this->error = 'Unable to decode HTTP "gzip" stream'; $this->success = false; } else { $this->body = $decoder->data; } break; case 'deflate': if (($decompressed = gzinflate($this->body)) !== false) { $this->body = $decompressed; } else { if (($decompressed = gzuncompress($this->body)) !== false) { $this->body = $decompressed; } else { if (function_exists('gzdecode') && ($decompressed = gzdecode($this->body)) !== false) { $this->body = $decompressed; } else { $this->error = 'Unable to decode HTTP "deflate" stream'; $this->success = false; } } } break; default: $this->error = 'Unknown content coding'; $this->success = false; } } } } else { $this->error = 'fsocket timed out'; $this->success = false; } fclose($fp); } } } else { $this->method = SIMPLEPIE_FILE_SOURCE_LOCAL | SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS; if (!($this->body = file_get_contents($url))) { $this->error = 'file_get_contents could not read the file'; $this->success = false; } } }
protected function searchApi($token, $term, $resultType = 'mixed', $geoCode = '', $count = 15) { // Construct the URL to call $url = 'https://api.twitter.com/1.1/search/tweets.json'; $queryString = '?q=' . urlencode(trim($term)) . '&result_type=' . $resultType . '&count=' . $count . '&include_entities=true'; if ($geoCode != '') { $queryString .= '&geocode=' . $geoCode; } $httpOptions = array(CURLOPT_TIMEOUT => 20, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_HTTPHEADER => array('GET /1.1/search/tweets.json' . $queryString . 'HTTP/1.1', 'Host: api.twitter.com', 'Authorization: Bearer ' . $token), CURLOPT_USERAGENT => 'Xibo Twitter Module', CURLOPT_HEADER => false, CURLINFO_HEADER_OUT => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => $url . $queryString); // Proxy support if (Config::GetSetting('PROXY_HOST') != '' && !Config::isProxyException($url)) { $httpOptions[CURLOPT_PROXY] = Config::GetSetting('PROXY_HOST'); $httpOptions[CURLOPT_PROXYPORT] = Config::GetSetting('PROXY_PORT'); if (Config::GetSetting('PROXY_AUTH') != '') { $httpOptions[CURLOPT_PROXYUSERPWD] = Config::GetSetting('PROXY_AUTH'); } } Debug::Audit('Calling API with: ' . $url . $queryString); $curl = curl_init(); curl_setopt_array($curl, $httpOptions); $result = curl_exec($curl); // Get the response headers $outHeaders = curl_getinfo($curl); if ($outHeaders['http_code'] == 0) { // Unable to connect Debug::Error('Unable to reach twitter api.'); return false; } else { if ($outHeaders['http_code'] != 200) { Debug::Error('Twitter API returned ' . $outHeaders['http_code'] . ' status. Unable to proceed. Headers = ' . var_export($outHeaders, true)); // See if we can parse the error. $body = json_decode($result); Debug::Error('Twitter Error: ' . (isset($body->errors[0]) ? $body->errors[0]->message : 'Unknown Error')); return false; } } // Parse out header and body $body = json_decode($result); return $body; }
/** * Request from Yahoo API * @param $yql * @return array|bool */ private function request($yql) { // Encode the YQL and make the request $url = 'https://query.yahooapis.com/v1/public/yql?q=' . urlencode($yql) . '&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys'; //$url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22TEC.PA%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback='; $httpOptions = array(CURLOPT_TIMEOUT => 20, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_USERAGENT => 'Xibo Digital Signage', CURLOPT_HEADER => false, CURLINFO_HEADER_OUT => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => $url); // Proxy support if (\Config::GetSetting('PROXY_HOST') != '' && !\Config::isProxyException($url)) { $httpOptions[CURLOPT_PROXY] = \Config::GetSetting('PROXY_HOST'); $httpOptions[CURLOPT_PROXYPORT] = \Config::GetSetting('PROXY_PORT'); if (\Config::GetSetting('PROXY_AUTH') != '') { $httpOptions[CURLOPT_PROXYUSERPWD] = \Config::GetSetting('PROXY_AUTH'); } } $curl = curl_init(); curl_setopt_array($curl, $httpOptions); $result = curl_exec($curl); // Get the response headers $outHeaders = curl_getinfo($curl); if ($outHeaders['http_code'] == 0) { // Unable to connect \Debug::Error('Unable to reach API. No Host Found (HTTP Code 0). Curl Error = ' . curl_error($curl)); return false; } else { if ($outHeaders['http_code'] != 200) { \Debug::Error('API returned ' . $outHeaders['http_code'] . ' status. Unable to proceed. Headers = ' . var_export($outHeaders, true)); // See if we can parse the error. $body = json_decode($result); \Debug::Error('Error: ' . (isset($body->errors[0]) ? $body->errors[0]->message : 'Unknown Error')); return false; } } // Parse out header and body $body = json_decode($result, true); return $body['query']['results']; }
/** * Download a file * @param string $url * @param string $savePath */ public static function downloadFile($url, $savePath) { // Use CURL to download a file // Open the file handle $fileHandle = fopen($savePath, 'w+'); // Configure CURL with the file handle $httpOptions = array(CURLOPT_TIMEOUT => 50, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_USERAGENT => 'Xibo Digital Signage', CURLOPT_HEADER => false, CURLOPT_FOLLOWLOCATION => true, CURLOPT_URL => $url, CURLOPT_FILE => $fileHandle); // Proxy support if (Config::GetSetting('PROXY_HOST') != '' && !Config::isProxyException($url)) { $httpOptions[CURLOPT_PROXY] = Config::GetSetting('PROXY_HOST'); $httpOptions[CURLOPT_PROXYPORT] = Config::GetSetting('PROXY_PORT'); if (Config::GetSetting('PROXY_AUTH') != '') { $httpOptions[CURLOPT_PROXYUSERPWD] = Config::GetSetting('PROXY_AUTH'); } } $curl = curl_init(); // Set our options curl_setopt_array($curl, $httpOptions); // Exec saves the file curl_exec($curl); // Close the curl connection curl_close($curl); // Close the file handle fclose($fileHandle); }