Jump to content
Killersites Forums

Can Someone Help Me Convert Python Code To Php?


Recommended Posts

Hello guys, i am trying to create an api request but the problem i have is that the code is not written in PHP. they only have Ruby and Python samples.

 

can someone please help me so that i can create a request to the api with PHP.

 

import hashlib
import hmac
import urllib2
import time

API_KEY = 'YOUR-API-KEY'
API_SECRET = 'YOUR-API-SECRET'

def get_http(url, body=None):
 opener = urllib2.build_opener()
 nonce = int(time.time() * 1e6)
 message = str(nonce) + url + ('' if body is None else body)
 signature = hmac.new(API_SECRET, message, hashlib.sha256).hexdigest()
 opener.addheaders = [('ACCESS_KEY', API_KEY),
                      ('ACCESS_SIGNATURE', signature),
                      ('ACCESS_NONCE', nonce)]
 try:
   return opener.open(urllib2.Request(url, body))
 except urllib2.HTTPError as e:
   print e
   return e

print get_http('https://thewebsite.com/api/v1/account/').read()

Link to post
Share on other sites
  • 1 month later...

Hi,

 

I just noticed this post now - sorry. Just in case you are still having problems and for others to see ... So, not knowing Python, I would just give up!

 

;)

 

... Just kidding!

 

Ok, so what I would do is find what each of these methods do, for instance:

 

urllib2:

 

http://docs.python.org/2/howto/urllib2.html

 

And then just rebuild the overall flow of this code. Once you've done these two things, you should be able to source out the PHP functions that do the same.

 

So, having only looked at Python 8000 years ago, I would say that the code is making an HTTP Get request and sending encoded HTTP headers.

 

Not sure since I have no idea of the context.

 

Stefan

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...