我需要連接到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