compcentral
compcentral

Reputation: 1185

Convert Ruby Code To PHP (Vicurex API)

I'm stuck trying to convert some sample Ruby API code from https://vircurex.com/welcome/api?locale=en to PHP. Here is the Ruby Code provided:

t = Time.now.gmtime.strftime("%Y-%m-%dT%H:%M:%S");
trx_id = Digest::SHA2.hexdigest("#{t}-#{rand}")
user_name = "MY_USER_NAME"
secret_word = "123456789"
tok = Digest::SHA2.hexdigest("#{secret_word};#{user_name};#{t};#{trx_id};create_order;sell;10;btc;50;nmc")
Order.call_https("https://vircurex.com", "/api/create_order.json?account=#{user_name}&id=#{trx_id}&token=#{tok}&timestamp=#{t}&ordertype=sell&amount=10&currency1=btc&unitprice=50&currency2=nmc")

def self.call_https(my_url,my_params)
  uri = URI.parse(my_url)
  http = Net::HTTP.new(uri.host, '443')
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  response=""
  resp=""
  http.start do |http|
    cmd = my_params
    req = Net::HTTP::Get.new(cmd)
    response = http.request(req)
    resp = response.body
  end
  return ActiveSupport::JSON.decode(resp)
end

Here is what I attempted to come up with so far in PHP, but i know nothing of Ruby, so it is hard to figure out what the original code is doing:

date_default_timezone_set("UTC");
$t = date("Y-m-d H:i:s", time());
$trx_id = hash("sha256", $t."-".rand()); // i think this is wrong
$user_name = "MY_USER_NAME";
$secret_word = "123456789";
$tok = hash("sha256", $secret_word.";".$user_name.";".$t.";".$trx_id.";create_order;sell;10;btc;50;nmc");
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "https://vircurex.com/api/create_order.json?account=$username&id=$trx_id&token=$tok&timestamp=$t&ordertype=sell&amount=10&currency1=btc&unitprice=50&currency2=nmc");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$resp = curl_exec($ch);
return json_decode($resp);

Can someone familiar with both languages please help me out? Thanks!

I get the following response with my code:

stdClass Object
(
    [status] => 8003
    [statustxt] => Authentication failed
)

So obviously, something is not being translated correctly. I just need to generate some working PHP code to use with the API listed. You can create an account to test the code if you like.

Upvotes: 0

Views: 455

Answers (1)

carmine
carmine

Reputation: 1655

The problem is on two lines:

First, change this line:

$t = date("Y-m-d H:i:s", time());

to:

$t = date("Y-m-d\TH:i:s", time());

And second, fix this:

curl_setopt($ch, CURLOPT_URL, "https://vircurex.com/api/create_order.json?account=$username&id=$trx_id&token=$tok&timestamp=$t&ordertype=sell&amount=10&currency1=btc&unitprice=50&currency2=nmc");

by changing $username param into $user_name:

curl_setopt($ch, CURLOPT_URL, "https://vircurex.com/api/create_order.json?account=$user_name&id=$trx_id&token=$tok&timestamp=$t&ordertype=sell&amount=10&currency1=btc&unitprice=50&currency2=nmc");

I successfully received a valid response from Vircurex with another API call, as the create_order API is currently disabled as reported on Vircurex site. In any case, I've had no authentication problems.

Upvotes: 1

Related Questions