我需要连接到RESTful API。公司给我连接到他们的API的唯一例子就是Python中的一个例子。我不懂这门语言,但对PHP很满意。有没有办法可以用cuRL和/或PHP来做到这一点?将python API示例转换为PHP
这里是在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
+1为了确保它是可能的,但没有这样做。 – 2013-03-06 00:59:10
谢谢,我经常使用curl,但是使用这个APIs auth需要时间戳和散列api key来发回一个响应代码,我输了。我想我确实需要将此代码转换为PHP或基于此代码的卷曲示例。 – 2013-03-06 01:08:55