我正在尝试创建一个脚本,该脚本将登录到 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)
提前致谢!
最佳答案
一些研究表明this .
$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而不是文件套接字。
关于php - 如何通过 HTTP header 使用另一台服务器登录 PHP 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11628488/