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: xmlspecialchars(string $text)
- //
- // Wraps htmlspecialchars and makes it work for xml
- // Does the following replacement:
- // < => <
- // > => >
- // " => "e;
- // ' => '
- // & => &
- ///////////////////////////////////////////////////////////////////////
- function xmlspecialchars($text) {
- return str_replace(''', ''', htmlspecialchars($text, ENT_QUOTES, 'UTF-8'));
- }
- // take an array and return an xml formatted string which represents it
- function _array_to_xml($array, $num_prefix = "num_") {
- $return = '';
- if (!is_array($array)) {
- return xmlspecialchars($array);
- //return html_entities($array);
- }
- else {
- foreach($array as $key=>$val){
- if (is_numeric($key)) { // can't have an element name that's numeric
- $return .= "\n" . _array_to_xml($val, "num_" . $num_prefix);
- }
- else {
- // 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
- // the input arrays in such a way that made sense, however ... will have to think about it.
- //$tag_begin = preg_replace("/_::(\d)+::$/"," id=\"$1\"",$key); // slight hack to get xml output formatted correctly
- //$tag_end = preg_replace("/_::(\d)+::$/","",$key); // slight hack to get xml output formatted correctly
- //$return .= str_repeat(" ",strlen($num_prefix)) . "<".$tag_begin.">"._array_to_xml($val, "num_" . $num_prefix)."</".$tag_end.">\n";
- // 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
- // json converter function below
- $key = preg_replace("/_::(\d)+::$/", "", $key, -1, $replacement_count); // slight hack to get xml output formatted correctly
- if ($replacement_count) {
- $return .= str_repeat(" ",strlen($num_prefix)) . "<".$key.">\n"._array_to_xml($val, "num_" . $num_prefix)."</".$key.">\n";
- }
- else {
- $return .= str_repeat(" ",strlen($num_prefix)) . "<".$key.">"._array_to_xml($val, "num_" . $num_prefix)."</".$key.">\n";
- }
- }
- }
- }
- if (!empty($return)) {
- return "$return\n" . str_repeat(" ",strlen($num_prefix) - 4);
- }
- }
- // 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 {
- echo "Key: ".$key." Data: ".$data."<br />";
- }
- }
- }
- //$result = api_query("getinfo");
- //$result = api_query("getmarkets");
- //$result = api_query("mytransactions");
- //$result = api_query("markettrades", array("marketid" => 26));
- // 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>";
- convertDates($result);
- // initializing or creating array
- //$array = array(your array data);
- // 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('test.xml');
- //$xml = new DOMDocument();
- //$xml = new SimpleXMLElement('<root/>');
- //array_walk_recursive($result, array ($xml, 'addChild'));
- //array_walk($result, array ($xml, 'addChild'));
- //print $xml->asXML();
- //$xml->asXML('test.xml');
- //$xml->save("test.xml");
- //$xml = new SimpleXMLElement('<root/>');
- //array_walk_rescursive($result, array ($xml, 'addChild'));
- //$xml = new DOMDocument();
- //$xml_album = $xml->createElement("Album");
- //$xml_track = $xml->createElement("Track");
- //$xml_album->appendChild( $xml_track );
- //$xml->appendChild( $xml_album );
- //$xml = _array_to_xml($result);
- //print $xml->asXML();
- //$xml = new SimpleXMLElement('<root/>');
- //$xml = _array_to_xml($result);
- //print $xml;
- //print $xml->asXML();
- //$xml->asXML('test.xml');
- //$xml->save("test.xml");
Advertisement
Add Comment
Please, Sign In to add comment