예제 #1
0
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;
}
예제 #2
0
파일: 796.php 프로젝트: phpsource/openclerk
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'];
}
예제 #3
0
function cryptostocks_api($key, $email, $method)
{
    $token_id = rand(0, 0xffff);
    $request_time = time();
    $token = $key . ";" . $email . ";" . $request_time . ";" . $token_id . ";" . $method;
    $url = url_add("https://cryptostocks.com/api/" . urlencode($method) . ".json", array('account' => $email, 'id' => $token_id, 'token' => hash('sha256', $token), 'timestamp' => $request_time));
    $content = crypto_get_contents(crypto_wrap_url($url));
    if (!$content) {
        throw new ExternalAPIException("API returned empty data");
    }
    $data = crypto_json_decode($content);
    return $data;
}
<?php

/**
 * Crypto-Trade securities ticker job.
 */
$exchange = "securities_crypto-trade";
// get the relevant security
$q = db()->prepare("SELECT * FROM securities_cryptotrade WHERE id=?");
$q->execute(array($job['arg_id']));
$security = $q->fetch();
if (!$security) {
    throw new JobException("Cannot find a {$exchange} security " . $job['arg_id'] . " for user " . $job['user_id']);
}
$cur1 = $security['currency'];
$cur2 = strtolower($security['name']);
$rates = crypto_json_decode(crypto_get_contents(crypto_wrap_url("https://crypto-trade.com/api/1/ticker/" . $cur2 . "_" . $cur1)));
if (!isset($rates['data']['max_bid'])) {
    if (isset($rates['error'])) {
        throw new ExternalAPIException("Could not find {$cur1}/{$cur2} rate for {$exchange}: " . htmlspecialchars($rates['error']));
    }
    throw new ExternalAPIException("No {$cur1}/{$cur2} rate for {$exchange}");
}
crypto_log("Security {$cur1}/{$cur2} balance: " . $rates['data']['max_bid']);
// insert new balance
insert_new_balance($job, $security, 'securities_crypto-trade', $security['currency'], $rates['data']['max_bid']);
예제 #5
0
<?php

/**
 * 796 security value job.
 * Retrieves the current 'bid' value for a particular security.
 */
$exchange = "securities_796";
$currency = 'btc';
// get the relevant address
$q = db()->prepare("SELECT * FROM securities_796 WHERE id=?");
$q->execute(array($job['arg_id']));
$account = $q->fetch();
if (!$account) {
    throw new JobException("Cannot find a {$exchange} account " . $job['arg_id']);
}
$content = crypto_get_contents(crypto_wrap_url('http://api.796.com/v3/stock/ticker.html?type=' . urlencode($account['api_name'])));
if (!$content) {
    throw new ExternalAPIException("API returned empty data");
}
$data = crypto_json_decode($content);
// also available: last, high, low, vol, buy, sell
if (!isset($data['ticker']['last'])) {
    throw new ExternalAPIException("External API returned no last price");
}
$balance = $data['ticker']['last'];
crypto_log("Last price for " . htmlspecialchars($account['name']) . ": " . $balance);
insert_new_balance($job, $account, $exchange, $currency, $balance);
예제 #6
0
$account = $q->fetch();
if (!$account) {
    throw new JobException("Cannot find a {$exchange} account " . $job['arg_id'] . " for user " . $job['user_id']);
}
// first, get balances
$data = crypto_json_decode(crypto_get_contents(crypto_wrap_url('https://www.litecoininvest.com/api/act?key=' . urlencode($account['api_key']))), false, true);
if (!isset($data['balance'][get_currency_abbr($currency)])) {
    throw new ExternalAPIException("No " . get_currency_abbr($currency) . " balance found");
}
$wallet = $data['balance'][get_currency_abbr($currency)];
// -- and now get securities --
// set is_recent=0 for all old security instances for this user
$q = db()->prepare("UPDATE securities SET is_recent=0 WHERE user_id=? AND exchange=? AND account_id=?");
$q->execute(array($job['user_id'], $exchange, $account['id']));
$balance = 0;
$data = crypto_json_decode(crypto_get_contents(crypto_wrap_url('https://www.litecoininvest.com/api/private/portfolio?key=' . urlencode($account['api_key']))), false, true);
foreach ($data as $row) {
    $security = $row['ticker'];
    $bid = $row['bid'];
    // also available: avg_buy_price
    // make sure that a security definition exists
    $q = db()->prepare("SELECT * FROM securities_litecoininvest WHERE name=?");
    $q->execute(array($security));
    $security_def = $q->fetch();
    if (!$security_def) {
        // need to insert a new security definition, so we can later get its value
        // we can't calculate the value of this security yet
        crypto_log("No securities_litecoininvest definition existed for '" . htmlspecialchars($security) . "': adding in new definition");
        $q = db()->prepare("INSERT INTO securities_litecoininvest SET name=?");
        $q->execute(array($security));
        $security_def = array('id' => db()->lastInsertId());
예제 #7
0
<?php

/**
 * A batch script to get all current Litecoininvest securities and queue them up for ticker values,
 * so we will have historical data even if no user has the security yet.
 */
$exchange = "securities_litecoininvest";
$currency = 'ltc';
// get the API data
$json = crypto_json_decode(crypto_get_contents(crypto_wrap_url('https://www.litecoininvest.com/api/ticker')));
foreach ($json as $security => $data) {
    crypto_log("Parsing security '" . htmlspecialchars($security) . "'");
    // we now have a new value
    $balance = $data['bid'];
    // also available: ask, latest, outstanding, 24h_vol, etc
    // if this security has a balance of 0, then it's worthless and it's not really
    // worth saving into the database
    if ($balance == 0) {
        crypto_log("Security '" . htmlspecialchars($security) . "' had a bid of 0: ignoring");
        continue;
    }
    $q = db()->prepare("SELECT * FROM securities_litecoininvest WHERE name=?");
    $q->execute(array($security));
    $security_def = $q->fetch();
    if (!$security_def) {
        // need to insert a new security definition, so we can later get its value
        // we can't calculate the value of this security yet
        crypto_log("No securities_litecoininvest definition existed for '" . htmlspecialchars($security) . "': adding in new definition");
        $q = db()->prepare("INSERT INTO securities_litecoininvest SET name=?");
        $q->execute(array($security));
        $security_def = array('name' => $security, 'id' => db()->lastInsertId());