MBScurl - converting PHP to XOJO #halp!

[b]Ok [h]so… I’m programming API connected xojo apps for trading bitcoin… essentially.[/h]

Bittrex.com was no problem… because Christian was kind enough to explain exactly what I needed to do… And for the most part I understood it. Well I’ve moved on from bittrex.com to cryptsy.com and cryptsy is slightly more advanced style API connectivity…

Here’s Bittrex PHP API example[/b]

$apikey='xxx'; $apisecret='xxx'; $nonce=time(); $uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce; $sign=hash_hmac('sha512',$uri,$apisecret); $ch = curl_init($uri); curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign)); $execResult = curl_exec($ch); $obj = json_decode($execResult);

Here’s my code for Bittrex API written in XOJO (Working!)

[code] dim apikey as string = bittrexApiKey
dim apisecret as string = bittrexApiSecret
dim nonceDate as date
nonceDate = new date
dim nonce as string = str(nonceDate.TotalSeconds)

dim uri as string = query + apikey + otherData + "&nonce=" + nonce
dim uriMemBlock as MemoryBlock = uri
dim sign as string = EncodeHex(Crypto.HMAC(apiSecret, uriMemBlock, Crypto.Algorithm.SHA512), false)

dim c as new CURLSMBS

c.OptionURL = uri
c.SetOptionHTTPheader array("apisign:" + sign)
c.OptionVerbose = true
c.CollectDebugData = true
c.CollectOutputData = true
c.OptionSSLVerifyHost = 0
c.OptionSSLVerifyPeer = 0

dim e as integer = c.Perform

dim data as string = c.OutputData
return c.OutputData[/code]

Here’s Cryptsy PHP API example

[code]<?php

function api_query($method, array $req = array()) {
// API settings
$key = ‘’; // your API-key
$secret = ‘’; // 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://api.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;

}[/code]

Here’s my code… which I haven’t even tested yet cause I don’t understand $req = array() and I don’t understand the $post_data = http_build_query($req, ‘’, ‘&’); either…

  
  dim apikey as string = "29d843337f62fafae60FAKEKEY170a8a64e"
  dim apisecret as string = "616ef523d1606208041eFAKEKEY75703585b6c82bbf11713fbb470c722b116e4177fbeb29ad0"
  dim nonceDate as date
  nonceDate = new date
  dim nonce as string = str(nonceDate.TotalSeconds)
  
  
  dim uri as string = query + otherData + "&nonce=" + nonce
  dim uriMemBlock as MemoryBlock = uri
  dim sign as string = EncodeHex(Crypto.HMAC(apiSecret, uriMemBlock, Crypto.Algorithm.SHA512), false)
  
  dim c as new CURLSMBS
  
  c.OptionURL = uri
  c.OptionPostFields = 
  c.SetOptionHTTPheader array("Sign: " + sign, "Key: " + apiKey)
  c.OptionVerbose = true
  c.CollectDebugData = true
  c.CollectOutputData = true
  c.OptionSSLVerifyHost = 0
  c.OptionSSLVerifyPeer = 0
  
  dim e as integer = c.Perform
  
  dim data as string = c.OutputData
  
  return c.OutputData

If I could just understand those 2 things… I could probably get this working! Any help is awesome.

yes?

I once had a blog article on how to convert PHP calls to plugin calls for FileMaker
http://www.mbsplugins.de/archive/2014-07-24/Translate_PHP_script_with_CURL/monkeybreadsoftware_blog_archive

it’s similar for Xojo