Advertisement
Selzier

Untitled

Dec 14th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.18 KB | None | 0 0
  1. <?php
  2.  
  3. function api_query($method, array $req = array()) {
  4.         // API settings
  5.         $key = '5320375ed2ce85577d1a40fea5084f7973036197'; // your API-key
  6.         $secret = '4a41cd0a1b4f16b2b1789f02e19d17bca11ac8a672f68b57f1eec92b93e19e27fad47165bddfee00'; // your Secret-key
  7.  
  8.         $req['method'] = $method;
  9.         $mt = explode(' ', microtime());
  10.         $req['nonce'] = $mt[1];
  11.        
  12.         // generate the POST data string
  13.         $post_data = http_build_query($req, '', '&');
  14.  
  15.         $sign = hash_hmac("sha512", $post_data, $secret);
  16.  
  17.         // generate the extra headers
  18.         $headers = array(
  19.                 'Sign: '.$sign,
  20.                 'Key: '.$key,
  21.         );
  22.  
  23.         // our curl handle (initialize if required)
  24.         static $ch = null;
  25.         if (is_null($ch)) {
  26.                 $ch = curl_init();
  27.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  28.                 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Cryptsy API PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
  29.         }
  30.         curl_setopt($ch, CURLOPT_URL, 'https://www.cryptsy.com/api');
  31.         curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  32.         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  33.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  34.  
  35.         // run the query
  36.         $res = curl_exec($ch);
  37.  
  38.         if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
  39.         $dec = json_decode($res, true);
  40.         if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
  41.         return $dec;
  42. }
  43.  
  44. // function defination to convert array to xml
  45. function array_to_xml($array, &$xml_array) {
  46.     foreach($array as $key => $value) {
  47.         if(is_array($value)) {
  48.             if(!is_numeric($key)){
  49.                 $subnode = $xml_array->addChild("$key");
  50.                 array_to_xml($value, $subnode);
  51.             }
  52.             else{
  53.                 $subnode = $xml_array->addChild("item$key");
  54.                 array_to_xml($value, $subnode);
  55.             }
  56.         }
  57.         else {
  58.             $xml_array->addChild("$key","$value");
  59.         }
  60.     }
  61. }
  62.  
  63. // Function definition to convert string dates to unix time
  64. function convertDates($array) {
  65.     foreach($array as $key=>$data) {
  66.         if (is_array($data)) {
  67.             convertDates($data);
  68.        
  69.         } elseif(is_object($data)) {
  70.             convertDates($data);
  71.        
  72.         } else {
  73.            
  74.             if ($key == "datetime") {
  75.                 //echo "Data: ".$data."<br />";
  76.                 //echo "Unix time: ".strtotime($data )."<br />";
  77.                 $data = strtotime($data);
  78.  
  79.                 echo "Data: ".$data."<br />";
  80.                
  81.             }
  82.         }
  83.     }
  84. }
  85.  
  86. function unixDate($array) {
  87.  
  88.     foreach($array as $key => $data)
  89.     {
  90.         $array[$key]['datetime'] = strtotime($data);
  91.         echo "Unix time: ".$data."<br />";
  92.     }
  93. }
  94.  
  95.  
  96. //$result = api_query("getinfo");
  97.  
  98. //$result = api_query("getmarkets");
  99.  
  100. //$result = api_query("mytransactions");
  101.  
  102. $result = api_query("markettrades", array("marketid" => 71));
  103.  
  104. convertDates($result);
  105. //unixDate($result);
  106.  
  107. // Quark Orderbook
  108. //$result = api_query("marketorders", array("marketid" => 71));
  109.  
  110. //$result = api_query("mytrades", array("marketid" => 26, "limit" => 1000));
  111.  
  112. //$result = api_query("allmytrades");
  113.  
  114. //$result = api_query("myorders", array("marketid" => 26));
  115.  
  116. //$result = api_query("allmyorders");
  117.  
  118. //$result = api_query("createorder", array("marketid" => 26, "ordertype" => "Sell", "quantity" => 1000, "price" => 0.00031000));
  119.  
  120. //$result = api_query("cancelorder", array("orderid" => 139567));
  121.  
  122. //$result = api_query("calculatefees", array("ordertype" => 'Buy', 'quantity' => 1000, 'price' => '0.005'));
  123.  
  124. //echo "<pre>".print_r($result, true)."</pre>";
  125.  
  126.  
  127.  
  128. // creating object of SimpleXMLElement
  129. $xml_result = new SimpleXMLElement("<?xml version=\"1.0\"?><root></root>");
  130.  
  131. // function call to convert array to xml
  132. array_to_xml($result,$xml_result);
  133.  
  134. //print
  135. //print $xml_result->asXML();
  136.  
  137. //saving generated xml file
  138. $xml_result->asXML('test02.xml');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement