Selzier

Untitled

Dec 14th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.25 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.  
  45. ///////////////////////////////////////////////////////////////////////
  46. //  Function: xmlspecialchars(string $text)
  47. //  
  48. //  Wraps htmlspecialchars and makes it work for xml
  49. //  Does the following replacement:
  50. //    <       =>     &lt;
  51. //    >       =>     &gt;
  52. //    "       =>     &quote;
  53. //    '       =>     &apos;
  54. //    &       =>     &amp;
  55. ///////////////////////////////////////////////////////////////////////
  56. function xmlspecialchars($text) {
  57.     return str_replace('&#039;', '&apos;', htmlspecialchars($text, ENT_QUOTES, 'UTF-8'));
  58. }
  59.  
  60.  
  61.  
  62.  
  63. // take an array and return an xml formatted string which represents it
  64. function _array_to_xml($array, $num_prefix = "num_") {
  65.     $return = '';
  66.    
  67.     if (!is_array($array)) {
  68.         return xmlspecialchars($array);
  69.         //return html_entities($array);
  70.     }
  71.     else {
  72.        
  73.         foreach($array as $key=>$val){
  74.             if (is_numeric($key)) { // can't have an element name that's numeric
  75.                 $return .= "\n" . _array_to_xml($val, "num_" . $num_prefix);
  76.             }
  77.             else {
  78.                 // FIXME: JC: if we wanted to include attributes in the tags, this is an example of how we might do that... it would get messy to construct
  79.                 //            the input arrays in such a way that made sense, however ... will have to think about it.
  80.                 //$tag_begin = preg_replace("/_::(\d)+::$/"," id=\"$1\"",$key); // slight hack to get xml output formatted correctly
  81.                 //$tag_end = preg_replace("/_::(\d)+::$/","",$key); // slight hack to get xml output formatted correctly
  82.                 //$return .= str_repeat(" ",strlen($num_prefix)) . "<".$tag_begin.">"._array_to_xml($val, "num_" . $num_prefix)."</".$tag_end.">\n";
  83.                
  84.                 // FIXME: (jc) - the hack below will break JSON output if JSON format is chosen, need to implement similar logic here that we use in the
  85.                 //        json converter function below
  86.                 $key = preg_replace("/_::(\d)+::$/", "", $key, -1, $replacement_count); // slight hack to get xml output formatted correctly
  87.                 if ($replacement_count) {
  88.                     $return .= str_repeat(" ",strlen($num_prefix)) . "<".$key.">\n"._array_to_xml($val, "num_" . $num_prefix)."</".$key.">\n";
  89.                 }
  90.                 else {
  91.                     $return .= str_repeat(" ",strlen($num_prefix)) . "<".$key.">"._array_to_xml($val, "num_" . $num_prefix)."</".$key.">\n";
  92.                 }
  93.             }
  94.         }
  95.     }
  96.     if (!empty($return)) {
  97.         return "$return\n" . str_repeat(" ",strlen($num_prefix) - 4);
  98.     }
  99. }
  100.  
  101. // function defination to convert array to xml
  102. function array_to_xml($array, &$xml_array) {
  103.     foreach($array as $key => $value) {
  104.         if(is_array($value)) {
  105.             if(!is_numeric($key)){
  106.                 $subnode = $xml_array->addChild("$key");
  107.                 array_to_xml($value, $subnode);
  108.             }
  109.             else{
  110.                 $subnode = $xml_array->addChild("item$key");
  111.                 array_to_xml($value, $subnode);
  112.             }
  113.         }
  114.         else {
  115.             $xml_array->addChild("$key","$value");
  116.         }
  117.     }
  118. }
  119.  
  120. // Function definition to convert string dates to unix time
  121. function convertDates($array) {
  122.     foreach($array as $key=>$data) {
  123.         if (is_array($data)) {
  124.             convertDates($data);
  125.        
  126.         } elseif(is_object($data)) {
  127.             convertDates($data);
  128.        
  129.         } else {
  130.             echo "Key: ".$key." Data: ".$data."<br />";
  131.            
  132.         }
  133.     }
  134. }
  135.  
  136.  
  137. //$result = api_query("getinfo");
  138.  
  139. //$result = api_query("getmarkets");
  140.  
  141. //$result = api_query("mytransactions");
  142.  
  143. //$result = api_query("markettrades", array("marketid" => 26));
  144.  
  145. // Quark Orderbook
  146. $result = api_query("marketorders", array("marketid" => 71));
  147.  
  148. //$result = api_query("mytrades", array("marketid" => 26, "limit" => 1000));
  149.  
  150. //$result = api_query("allmytrades");
  151.  
  152. //$result = api_query("myorders", array("marketid" => 26));
  153.  
  154. //$result = api_query("allmyorders");
  155.  
  156. //$result = api_query("createorder", array("marketid" => 26, "ordertype" => "Sell", "quantity" => 1000, "price" => 0.00031000));
  157.  
  158. //$result = api_query("cancelorder", array("orderid" => 139567));
  159.  
  160. //$result = api_query("calculatefees", array("ordertype" => 'Buy', 'quantity' => 1000, 'price' => '0.005'));
  161.  
  162. //echo "<pre>".print_r($result, true)."</pre>";
  163.  
  164. convertDates($result);
  165.  
  166. // initializing or creating array
  167. //$array = array(your array data);
  168.  
  169. // creating object of SimpleXMLElement
  170. $xml_result = new SimpleXMLElement("<?xml version=\"1.0\"?><root></root>");
  171.  
  172. // function call to convert array to xml
  173. array_to_xml($result,$xml_result);
  174.  
  175. //print
  176. //print $xml_result->asXML();
  177.  
  178. //saving generated xml file
  179. $xml_result->asXML('test.xml');
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186. //$xml = new DOMDocument();
  187. //$xml = new SimpleXMLElement('<root/>');
  188. //array_walk_recursive($result, array ($xml, 'addChild'));
  189. //array_walk($result, array ($xml, 'addChild'));
  190. //print $xml->asXML();
  191.  
  192. //$xml->asXML('test.xml');
  193. //$xml->save("test.xml");
  194.  
  195. //$xml = new SimpleXMLElement('<root/>');
  196. //array_walk_rescursive($result, array ($xml, 'addChild'));
  197. //$xml = new DOMDocument();
  198. //$xml_album = $xml->createElement("Album");
  199. //$xml_track = $xml->createElement("Track");
  200. //$xml_album->appendChild( $xml_track );
  201. //$xml->appendChild( $xml_album );
  202.  
  203.  
  204. //$xml = _array_to_xml($result);
  205.  
  206. //print $xml->asXML();
  207. //$xml = new SimpleXMLElement('<root/>');
  208. //$xml = _array_to_xml($result);
  209. //print $xml;
  210. //print $xml->asXML();
  211.  
  212. //$xml->asXML('test.xml');
  213. //$xml->save("test.xml");
Advertisement
Add Comment
Please, Sign In to add comment