首頁  >  問答  >  主體

PHP中的等值函數是什麼,可以實作Python中的'requests.post'功能?

我需要將 python 程式碼翻譯為 PHP 的協助。腳本就是這樣設定的,以便與 API 伺服器通信,在這部分程式碼中使用 POST 登入。

Python 程式碼已經過測試並且可以工作,但是我無法確定應該以哪種方式將其準確地轉換為 PHP,因為我從 API 獲得了空響應,這意味著 - 錯誤。我懷疑使用了錯誤的 post 參數或 post 方法。

在 python 程式碼中,有一條註解說明了成功 API 回傳應該是什麼樣子。

編輯: var_dump($結果);傳回 bool(false) 且啟用錯誤報告後,會彈出此警告: 警告:file_get_contents(https://kodi.titlvi.com/api/subtitles/gettoken):無法開啟串流:HTTP請求失敗! 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

P粉495955986P粉495955986178 天前300

全部回覆(1)我來回復

  • P粉281089485

    P粉2810894852024-03-29 13:13:59

    param=dictionary 將參數放入 URL 查詢參數中,而不是 POST 資料中。

    伺服器需要 Content-length: 標頭,PHP 預設不傳送該標頭。

    為了在標頭中包含 \r\n,您必須使用雙引號,而不是單引號。

     $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);
    ?>

    回覆
    0
  • 取消回覆