簡體   English   中英

如何通過HTTP標頭使用其他服務器登錄PHP腳本?

[英]How to login a PHP script with another server via HTTP headers?

我正在嘗試創建一個腳本,該腳本將登錄到Icecast2服務器並獲取當前播放曲目的元數據。 我的問題是Icecast服務器通過基本的HTTP登錄受到用戶/密碼保護。

    <?php
        $fp = fsockopen("xxxxxxxx.com", 8000, $errno, $errstr, 30);
        if (!$fp) {
            echo "$errstr ($errno)<br />\n";
        } else {
            $out = "GET / HTTP/1.1\r\n";
            $out .= "Icy-MetaData:1\r\n";
            $out .= "Host: xxxxxxxx.com\r\n";
            $out .= "Connection: Close\r\n\r\n";

            fwrite($fp, $out);
            $arr = array();

            while (!feof($fp)) {
                $arr[] = fgets($fp, 128);
            }
            fclose($fp);
        }

        echo json_encode(trim(strip_tags($arr[73])));

?>

這是允許我在Icecast服務器受到密碼保護之前抓取元數據的腳本。

簡而言之,如何通過此腳本將用戶/密碼發送給Icecast服務器以成功登錄並獲取信息? (使用htpasswd)

提前致謝!

一些研究揭示了這一點

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt(CURLOPT_USERPWD, '[username]:[password]')

$data = curl_exec();
curl_close($ch);

但是,它需要使用cURL而不是文件套接字。

Wikipedia所述 ,您需要Authorization: Basic標頭和base64_encode() username:password

要添加的代碼:

$out .= "Authorization: Basic " . base64_encode($user .":". $password);

手動登錄以檢查頁面是否使用cookie來驗證用戶身份,如果可以,則可以使用標頭發送cookie,這應該允許您從受密碼保護的頁面檢索數據。

$array = array(
      'http'=>array(
        'header'=>"Cookie: *cookiedatahere*"
      )
);

$context = stream_context_create($array);

$url = *password protected data source*;
$raw = file_get_contents($url, false, $context);

暫無
暫無

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

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