Convert python API example to PHP
Question: I need to connect to a RESTful API. The only example the company gave me to
connect to their API is a example in Python. I do not understand the language
but am comfortable with PHP. Is there a way I can do this with cuRL and/or
PHP?

Here is the example in Python:

    
    
    import requests
    import hmac
    import hashlib
    import datetime as dt
    import simplejson as json
    import sys
    
    
    tech_prefix = '' #the Account Tech Prefix
    secret_key = '' #the API Key
    
    
    #creating URI info
    t = dt.datetime.utcnow().replace(microsecond=0)
    timestamp = t.isoformat()
    url_scheme = 'https'
    net_location = 'api.thesite.com'
    path = '/v1/available-tns/npas/'
    method = 'GET'
    ordered_query_params = ''
    body = ''
    body_md5 = ''
    canonical_uri = url_scheme + "://" + net_location + path + "\n" + ordered_query_params
    tokens = (
        timestamp,
        method,
        body_md5,
        canonical_uri
        )
    message_string = u'\n'.join(tokens).encode('utf-8')
    signature = hmac.new(secret_key, message_string, digestmod=hashlib.sha1).hexdigest()
    
    headers = {'X-Timestamp':timestamp}
    request_url = url_scheme + '://' + net_location + path + '?' + ordered_query_params #    append ordered query params here
    request = requests.get(request_url,auth=(tech_prefix,signature),headers=headers)
    print request
    


Answer: Yes, you can do this in PHP. This python code doesn't use any special python
libraries; it's just sending an HTTP request with specific headers and
specific auth info. Actually translating this code from python into PHP is
outside the scope of a typical StackOverflow answer, though.

