将 PHP 卷曲请求 API 转换为 python
Converting PHP Curl request API to python
有人可以让我将此 PHP API 代码转换为 Python。我尝试但失败了。我了解 python 中的基本请求,但无法在该请求中传递属性,我也对在请求中传递 cURL 数据感到困惑。
<?php
$apiKey = 'SOME_API_KEY';
$apiSecret = 'SOME_API_SECRET';
$message = array(
'message' => array(
'message' => 'How are you doing today?',
'chatBotID' => 6,
'timestamp' => time()
),
'user' => array(
'firstName' => 'Tugger',
'lastName' => 'Sufani',
'gender' => 'm',
'externalID' => 'abc-639184572'
)
);
// construct the data
$host = "https://www.personalityforge.com/api/chat/";
$messageJSON = json_encode($message);
$hash = hash_hmac('sha256', $messageJSON, $apiSecret);
$url = $host."?apiKey=".$apiKey."&hash=".$hash."&message=".urlencode($messageJSON);
// Make the call using cURL
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// make the call
$response = curl_exec($ch);
curl_close($ch);
echo 'Response JSON: '.$response.'<br>';
$responseObject = json_decode($response);
echo 'Response Object: <br>';
var_dump($responseObject);
?>
import requests
import hashlib
import time
import json
import hmac
apiKey = 'SOME_API_KEY'
apiSecret = 'SOME_API_SECRET'
message = {
'message': {
'message': 'How are you doing today?',
'chatBotID': 6,
'timestamp': time.time(),
},
'user': {
'firstName': 'Tugger',
'lastName': 'Sufani',
'gender': 'm',
'externalID': 'abc-639184572',
}
}
host = 'https://www.personalityforge.com/api/chat/index.php'
messageJSON = json.dumps(message)
hash_message = hmac.new(apiSecret.encode('utf-8'), messageJSON.encode('utf-8'), hashlib.sha256).hexdigest()
response = requests.get(host, {"apiKey": apiKey, "hash": hash_message, "message": messageJSON})
print('Response JSON: ', response.json())
这是您要找的吗?它给出了我需要一个有效的 api 密钥的响应,考虑到你给了假货,这是有道理的...
有人可以让我将此 PHP API 代码转换为 Python。我尝试但失败了。我了解 python 中的基本请求,但无法在该请求中传递属性,我也对在请求中传递 cURL 数据感到困惑。
<?php
$apiKey = 'SOME_API_KEY';
$apiSecret = 'SOME_API_SECRET';
$message = array(
'message' => array(
'message' => 'How are you doing today?',
'chatBotID' => 6,
'timestamp' => time()
),
'user' => array(
'firstName' => 'Tugger',
'lastName' => 'Sufani',
'gender' => 'm',
'externalID' => 'abc-639184572'
)
);
// construct the data
$host = "https://www.personalityforge.com/api/chat/";
$messageJSON = json_encode($message);
$hash = hash_hmac('sha256', $messageJSON, $apiSecret);
$url = $host."?apiKey=".$apiKey."&hash=".$hash."&message=".urlencode($messageJSON);
// Make the call using cURL
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// make the call
$response = curl_exec($ch);
curl_close($ch);
echo 'Response JSON: '.$response.'<br>';
$responseObject = json_decode($response);
echo 'Response Object: <br>';
var_dump($responseObject);
?>
import requests
import hashlib
import time
import json
import hmac
apiKey = 'SOME_API_KEY'
apiSecret = 'SOME_API_SECRET'
message = {
'message': {
'message': 'How are you doing today?',
'chatBotID': 6,
'timestamp': time.time(),
},
'user': {
'firstName': 'Tugger',
'lastName': 'Sufani',
'gender': 'm',
'externalID': 'abc-639184572',
}
}
host = 'https://www.personalityforge.com/api/chat/index.php'
messageJSON = json.dumps(message)
hash_message = hmac.new(apiSecret.encode('utf-8'), messageJSON.encode('utf-8'), hashlib.sha256).hexdigest()
response = requests.get(host, {"apiKey": apiKey, "hash": hash_message, "message": messageJSON})
print('Response JSON: ', response.json())
这是您要找的吗?它给出了我需要一个有效的 api 密钥的响应,考虑到你给了假货,这是有道理的...