> 백엔드 개발 > PHP 튜토리얼 > PHP 코드를 Java 코드로 변환

PHP 코드를 Java 코드로 변환

巴扎黑
풀어 주다: 2016-11-12 11:23:16
원래의
8159명이 탐색했습니다.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

<?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();

 

}

?>

로그인 후 복사

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿