Python 'requests.post' 在 PHP 中的等价物是什么?
What is the equivalent for Python 'requests.post' in PHP?
我需要有关将 python 代码翻译成 PHP 的帮助。该脚本以这种方式设置,因此它与 API 服务器通信,在这部分代码中使用 POST 登录。
Python 代码已经过测试并且可以正常工作,但是我无法确定应该以哪种方式将它准确地转换为 PHP,因为我从 [=38 得到空响应=],这意味着 - 错误。我怀疑使用了错误的 post 参数或 post 方法。
在 python 代码中,有一条注释说明成功 API return 应该是什么样子。
编辑:
var_dump($结果);是 returning bool(false) 并且启用错误报告后,会弹出此警告:
Warning: file_get_contents(https://kodi.titlovi.com/api/subtitles/gettoken): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /var/www/html/test.php on line 19 bool(false)
PHP - 当前脚本
<?php
error_reporting(-1);
ini_set('display_errors', 1);
$api_url = "https://kodi.titlovi.com/api/subtitles";
$username = "censored";
$password = "censored";
// sending user login request
$parameters = array('username' => $username, 'password' => $password, 'json' => true);
$options = array('http' => array(
'header' => 'Content-Type: application/x-www-form-urlencoded\r\n',
'method' => 'POST',
'content' => http_build_query($parameters)
));
$context = stream_context_create($options);
$result = file_get_contents($api_url.'/gettoken', false, $context);
var_dump($result);
?>
Python(工作示例)
api_url = 'https://kodi.titlovi.com/api/subtitles'
def handle_login(self):
"""
Method used for sending user login request.
OK return:
{
"ExpirationDate": datetime string (format: '%Y-%m-%dT%H:%M:%S.%f'),
"Token": string,
"UserId": integer,
"UserName": string
}
Error return: None
"""
logger('starting user login')
login_params = dict(username=self.username, password=self.password, json=True)
try:
response = requests.post('{0}/gettoken'.format(api_url), params=login_params)
logger('Response status: {0}'.format(response.status_code))
if response.status_code == requests.codes.ok:
resp_json = response.json()
logger('login response data: {0}'.format(resp_json))
return resp_json
elif response.status_code == requests.codes.unauthorized:
show_notification(get_string(32006))
return None
else:
return None
except Exception as e:
logger(e)
return None
param=dictionary
将参数放在 URL 查询参数中,而不是 POST 数据。
服务器需要 Content-length:
header,默认情况下 PHP 不会发送。
为了在 header 中包含 \r\n
,您必须使用双引号,而不是单引号。
<?php
error_reporting(-1);
ini_set('display_errors', 1);
$api_url = "https://kodi.titlovi.com/api/subtitles";
$username = "XXX";
$password = "YYY";
// sending user login request
$parameters = array('username' => $username, 'password' => $password, 'json' => True);
$options = array('http' => array(
'header' => "Content-Length: 0\r\n",
'method' => 'POST',
));
$context = stream_context_create($options);
$result = file_get_contents($api_url.'/gettoken?' . http_build_query($parameters), false, $context);
var_dump($result);
?>
我需要有关将 python 代码翻译成 PHP 的帮助。该脚本以这种方式设置,因此它与 API 服务器通信,在这部分代码中使用 POST 登录。
Python 代码已经过测试并且可以正常工作,但是我无法确定应该以哪种方式将它准确地转换为 PHP,因为我从 [=38 得到空响应=],这意味着 - 错误。我怀疑使用了错误的 post 参数或 post 方法。
在 python 代码中,有一条注释说明成功 API return 应该是什么样子。
编辑:
var_dump($结果);是 returning bool(false) 并且启用错误报告后,会弹出此警告:
Warning: file_get_contents(https://kodi.titlovi.com/api/subtitles/gettoken): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /var/www/html/test.php on line 19 bool(false)
PHP - 当前脚本
<?php
error_reporting(-1);
ini_set('display_errors', 1);
$api_url = "https://kodi.titlovi.com/api/subtitles";
$username = "censored";
$password = "censored";
// sending user login request
$parameters = array('username' => $username, 'password' => $password, 'json' => true);
$options = array('http' => array(
'header' => 'Content-Type: application/x-www-form-urlencoded\r\n',
'method' => 'POST',
'content' => http_build_query($parameters)
));
$context = stream_context_create($options);
$result = file_get_contents($api_url.'/gettoken', false, $context);
var_dump($result);
?>
Python(工作示例)
api_url = 'https://kodi.titlovi.com/api/subtitles'
def handle_login(self):
"""
Method used for sending user login request.
OK return:
{
"ExpirationDate": datetime string (format: '%Y-%m-%dT%H:%M:%S.%f'),
"Token": string,
"UserId": integer,
"UserName": string
}
Error return: None
"""
logger('starting user login')
login_params = dict(username=self.username, password=self.password, json=True)
try:
response = requests.post('{0}/gettoken'.format(api_url), params=login_params)
logger('Response status: {0}'.format(response.status_code))
if response.status_code == requests.codes.ok:
resp_json = response.json()
logger('login response data: {0}'.format(resp_json))
return resp_json
elif response.status_code == requests.codes.unauthorized:
show_notification(get_string(32006))
return None
else:
return None
except Exception as e:
logger(e)
return None
param=dictionary
将参数放在 URL 查询参数中,而不是 POST 数据。
服务器需要 Content-length:
header,默认情况下 PHP 不会发送。
为了在 header 中包含 \r\n
,您必须使用双引号,而不是单引号。
<?php
error_reporting(-1);
ini_set('display_errors', 1);
$api_url = "https://kodi.titlovi.com/api/subtitles";
$username = "XXX";
$password = "YYY";
// sending user login request
$parameters = array('username' => $username, 'password' => $password, 'json' => True);
$options = array('http' => array(
'header' => "Content-Length: 0\r\n",
'method' => 'POST',
));
$context = stream_context_create($options);
$result = file_get_contents($api_url.'/gettoken?' . http_build_query($parameters), false, $context);
var_dump($result);
?>