0

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.

15
  • First, you want just hash, not hash_hmac, where you set $trx_id - that one is just a plain old hash, no secret key. Commented Dec 4, 2013 at 4:21
  • Have you tried running the Ruby to verify that it works as provided? Commented Dec 4, 2013 at 4:21
  • I tried changing to hash() but still same results. I don't have any way to test the Ruby code, but I know that others use it. Commented Dec 4, 2013 at 4:23
  • I think the problem is how i pass the url parameters... can't tell if they should be POST or GET? Commented Dec 4, 2013 at 4:28
  • It's a query string, not a request body, and the fact that the Ruby is using Net::HTTP::Get makes it an HTTP GET request. Commented Dec 4, 2013 at 4:29

1 Answer 1

1
+50

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! I knew I had to be missing something simple. I can't believe i made the "user_name" mistake. Code works fine tho now. Hopefully this will help others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.