国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

PHP中的等效函數(shù)是什么,可以實現(xiàn)Python中的'requests.post'功能?
P粉495955986
P粉495955986 2024-03-28 14:48:16
0
1
502

我需要有關將 python 代碼翻譯為 PHP 的幫助。腳本就是這樣設置的,以便與 API 服務器通信,在這部分代碼中使用 POST 登錄。

Python 代碼已經(jīng)過測試并且可以工作,但是我無法確定應該以哪種方式將其準確地轉(zhuǎn)換為 PHP,因為我從 API 得到了空響應,這意味著 - 錯誤。我懷疑使用了錯誤的 post 參數(shù)或 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粉495955986
P粉495955986

全部回復(1)
P粉281089485

param=dictionary 將參數(shù)放入 URL 查詢參數(shù)中,而不是 POST 數(shù)據(jù)中。

服務器需要 Content-length: 標頭,PHP 默認情況下不發(fā)送該標頭。

為了在標頭中包含 \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);
?>
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板