Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function api_query($method, array $req = array()) {
- // API settings
- $key = '5320375ed2ce85577d1a40fea5084f7973036197'; // your API-key
- $secret = '4a41cd0a1b4f16b2b1789f02e19d17bca11ac8a672f68b57f1eec92b93e19e27fad47165bddfee00'; // your Secret-key
- $req['method'] = $method;
- $mt = explode(' ', microtime());
- $req['nonce'] = $mt[1];
- // generate the POST data string
- $post_data = http_build_query($req, '', '&');
- $sign = hash_hmac("sha512", $post_data, $secret);
- // generate the extra headers
- $headers = array(
- 'Sign: '.$sign,
- 'Key: '.$key,
- );
- // 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, 'Mozilla/4.0 (compatible; Cryptsy API PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
- }
- curl_setopt($ch, CURLOPT_URL, 'https://www.cryptsy.com/api');
- 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('Could not get reply: '.curl_error($ch));
- $dec = json_decode($res, true);
- if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
- return $dec;
- }
- // function defination to convert array to xml
- function array_to_xml($array, &$xml_array) {
- foreach($array as $key => $value) {
- if(is_array($value)) {
- if(!is_numeric($key)){
- $subnode = $xml_array->addChild("$key");
- array_to_xml($value, $subnode);
- }
- else{
- $subnode = $xml_array->addChild("item$key");
- array_to_xml($value, $subnode);
- }
- }
- else {
- $xml_array->addChild("$key","$value");
- }
- }
- }
- // Function definition to convert string dates to unix time
- function convertDates($array) {
- foreach($array as $key=>$data) {
- if (is_array($data)) {
- convertDates($data);
- } elseif(is_object($data)) {
- convertDates($data);
- } else {
- if ($key == "datetime") {
- //echo "Data: ".$data."<br />";
- //echo "Unix time: ".strtotime($data )."<br />";
- $data = strtotime($data);
- echo "Data: ".$data."<br />";
- }
- }
- }
- }
- function unixDate($array) {
- foreach($array as $key => $data)
- {
- $array[$key]['datetime'] = strtotime($data);
- echo "Unix time: ".$data."<br />";
- }
- }
- //$result = api_query("getinfo");
- //$result = api_query("getmarkets");
- //$result = api_query("mytransactions");
- $result = api_query("markettrades", array("marketid" => 71));
- convertDates($result);
- //unixDate($result);
- // Quark Orderbook
- //$result = api_query("marketorders", array("marketid" => 71));
- //$result = api_query("mytrades", array("marketid" => 26, "limit" => 1000));
- //$result = api_query("allmytrades");
- //$result = api_query("myorders", array("marketid" => 26));
- //$result = api_query("allmyorders");
- //$result = api_query("createorder", array("marketid" => 26, "ordertype" => "Sell", "quantity" => 1000, "price" => 0.00031000));
- //$result = api_query("cancelorder", array("orderid" => 139567));
- //$result = api_query("calculatefees", array("ordertype" => 'Buy', 'quantity' => 1000, 'price' => '0.005'));
- //echo "<pre>".print_r($result, true)."</pre>";
- // creating object of SimpleXMLElement
- $xml_result = new SimpleXMLElement("<?xml version=\"1.0\"?><root></root>");
- // function call to convert array to xml
- array_to_xml($result,$xml_result);
- //print
- //print $xml_result->asXML();
- //saving generated xml file
- $xml_result->asXML('test02.xml');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement