簡體   English   中英

PHP 中的 Python 'requests.post' 的等價物是什么?

[英]What is the equivalent for Python 'requests.post' in PHP?

我需要有關將 python 代碼轉換為 PHP 的幫助。腳本以這種方式設置,因此它與 API 服務器通信,在這部分代碼中使用 POST 登錄。

Python 代碼已經過測試並且可以正常工作,但是我無法確定應該以哪種方式將它准確地轉換為 PHP,因為我從 API 得到空響應,這意味着 - 錯誤。 我懷疑使用了錯誤的發布參數或發布方法。

在 python 代碼中,有一條注釋說明成功 API 返回應該是什么樣子。

編輯: var_dump($result); 正在返回 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) 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);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM