Home  >  Article  >  Backend Development  >  Convert PHP code to java code

Convert PHP code to java code

巴扎黑
巴扎黑Original
2016-11-12 11:23:168067browse

<?php 

function bter_query($path, array $req = array()) { 
// API settings, add your Key and Secret at here 
$key = &#39;&#39;; 
$secret = &#39;&#39;; 

// generate a nonce to avoid problems with 32bits systems 
$mt = explode(&#39; &#39;, microtime()); 
$req[&#39;nonce&#39;] = $mt[1].substr($mt[0], 2, 6); 

// generate the POST data string 
$post_data = http_build_query($req, &#39;&#39;, &#39;&&#39;); 
$sign = hash_hmac(&#39;sha512&#39;, $post_data, $secret); 

// generate the extra headers 
$headers = array( 
&#39;KEY: &#39;.$key, 
&#39;SIGN: &#39;.$sign, 
); 

//!!! please set Content-Type to application/x-www-form-urlencoded if it&#39;s not the default value 

// curl handle (initialize if required) 
static $ch = null; 
if (is_null($ch)) { 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERAGENT, 
&#39;Mozilla/4.0 (compatible; Bter PHP bot; &#39;.php_uname(&#39;a&#39;).&#39;; PHP/&#39;.phpversion().&#39;)&#39; 
); 
} 
curl_setopt($ch, CURLOPT_URL, &#39;https://bter.com/api/&#39;.$path); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 

// run the query 
$res = curl_exec($ch); 

if ($res === false) throw new Exception(&#39;Curl error: &#39;.curl_error($ch)); 
//echo $res; 
$dec = json_decode($res, true); 
if (!$dec) throw new Exception(&#39;Invalid data: &#39;.$res); 
return $dec; 
} 

function get_top_rate($pair, $type=&#39;BUY&#39;) { 
$rate = 0; 

// our curl handle (initialize if required) 
static $ch = null; 
if (is_null($ch)) { 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERAGENT, 
&#39;Mozilla/4.0 (compatible; Bter PHP bot; &#39;.php_uname(&#39;a&#39;).&#39;; PHP/&#39;.phpversion().&#39;)&#39; 
); 
} 
curl_setopt($ch, CURLOPT_URL, &#39;https://bter.com/api/1/depth/&#39;.$pair); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 

// run the query 
$res = curl_exec($ch); 
if ($res === false) throw new Exception(&#39;Could not get reply: &#39;.curl_error($ch)); 
//echo $res; 
$dec = json_decode($res, true); 
if (!$dec) throw new Exception(&#39;Invalid data: &#39;.$res); 

if (strtoupper($type) == &#39;BUY&#39;) { 
$r =  $dec[&#39;bids&#39;][0]; 
$rate = $r[0]; 
} else  { 
$r = end($dec[&#39;asks&#39;]); 
$rate = $r[0]; 
} 

return $rate; 
} 


try { 
// example 1: get funds 
var_dump(bter_query(&#39;1/private/getfunds&#39;)); 

// example 2: place a buy order 
$pair = &#39;ltc_btc&#39;; 
$type = &#39;buy&#39;; 
$rate = get_top_rate($pair, $type) * 1.01; 
var_dump(bter_query(&#39;1/private/placeorder&#39;, 
array( 
&#39;pair&#39; => "$pair", 
&#39;type&#39; => "$type", 
&#39;rate&#39; => "$rate", 
&#39;amount&#39; => &#39;0.01&#39;, 
) 
  ) 
); 

// example 3: cancel an order 
var_dump(bter_query(&#39;1/private/cancelorder&#39;, array(&#39;order_id&#39; => 125811))); 

// example 4: get order status 
var_dump(bter_query(&#39;1/private/getorder&#39;, array(&#39;order_id&#39; => 15088))); 

//example 5: list all open orders 
var_dump(bter_query(&#39;1/private/orderlist&#39;)); 

} catch (Exception $e) { 
echo "Error:".$e->getMessage(); 

} 
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP file traversalNext article:PHP file traversal