function havelock_query($url, array $req = array()) { // our curl handle (initialize if required) $ch = crypto_curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Havelock PHP client; ' . php_uname('s') . '; PHP/' . phpversion() . ')'); curl_setopt($ch, CURLOPT_URL, crypto_wrap_url($url)); curl_setopt($ch, CURLOPT_POSTFIELDS, $req); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // run the query $res = curl_exec($ch); if ($res === false) { throw new ExternalAPIException('Could not get reply: ' . curl_error($ch)); } if ($res === "Access denied") { throw new ExternalAPIException("API response: Access denied"); } $temp = json_decode($res, true); if (is_array($temp) && !$temp) { throw new ExternalAPIException("Havelock API returned an empty array"); } $dec = crypto_json_decode($res); if (isset($dec['message'])) { throw new ExternalAPIException(htmlspecialchars($dec['message'])); } return $dec; }
function xchange796_query($appid, $apikey, $secretkey, $url) { // from https://796.com/wiki.html $timestamp = time(); $params = array('appid' => $appid, 'apikey' => $apikey, 'secretkey' => $secretkey, 'timestamp' => $timestamp); ksort($params); // "be careful that the sequence is quite important" $param_uri = http_build_query($params, '', '&'); $sig = base64_encode(hash_hmac('sha1', $param_uri, $secretkey)); $token_url = url_add("https://796.com/oauth/token", array('appid' => $appid, 'timestamp' => $timestamp, 'apikey' => $apikey, 'secretkey' => $secretkey, 'sig' => $sig)); // our curl handle (initialize if required) $ch = crypto_curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; 796 PHP client; ' . php_uname('s') . '; PHP/' . phpversion() . ')'); curl_setopt($ch, CURLOPT_URL, crypto_wrap_url($token_url)); // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // run the query $res = curl_exec($ch); if ($res === false) { throw new ExternalAPIException('Could not get reply: ' . curl_error($ch)); } $dec = crypto_json_decode($res, "in authentication"); if (isset($dec['errno']) && $dec['errno']) { throw new ExternalAPIException("Could not get OAuth Token: " . htmlspecialchars($dec['msg'])); } if (!isset($dec['data']['access_token'])) { throw new ExternalAPIException("No access token provided"); } $token = $dec['data']['access_token']; crypto_log("Obtained OAuth token"); // now, call the given URL // 796 has a bug where the token can't be urlencoded again, so we can't use url_add() (though we should) $destination_url = $url . "?access_token=" . $token; // our curl handle (initialize if required) $ch = crypto_curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; 796 PHP client; ' . php_uname('s') . '; PHP/' . phpversion() . ')'); curl_setopt($ch, CURLOPT_URL, crypto_wrap_url($destination_url)); // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // run the query $res = curl_exec($ch); if ($res === false) { throw new ExternalAPIException('Could not get reply: ' . curl_error($ch)); } $dec = crypto_json_decode($res, "in request"); if (isset($dec['errno']) && $dec['errno']) { throw new ExternalAPIException("Error in reply: " . htmlspecialchars($dec['msg'])); } if (!isset($dec['data'])) { throw new ExternalAPIException("No data in reply"); } return $dec['data']; }
/** * Wraps {@link #file_get_contents()} with timeout information etc. * May throw a {@link ExternalAPIException} if something unexpected occured. */ function crypto_get_contents($url, $options = array()) { \Openclerk\Events::trigger('curl_start', $url); // normally file_get_contents is OK, but if URLs are down etc, the timeout has no value and we can just stall here forever // this also means we don't have to enable OpenSSL on windows for file_get_contents('https://...'), which is just a bit of a mess $ch = crypto_curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Openclerk PHP client; ' . php_uname('s') . '; PHP/' . phpversion() . ')'); curl_setopt($ch, CURLOPT_URL, $url); // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate"); // enable gzip decompression if necessary curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); foreach ($options as $key => $value) { curl_setopt($ch, $key, $value); } // run the query $res = curl_exec($ch); \Openclerk\Events::trigger('curl_end', $url); if ($res === false) { throw new ExternalAPIException('Could not get reply: ' . curl_error($ch)); } crypto_check_response($res); return $res; }