2

我有广播管理员,我需要从中读取 xml,它看起来像这样

http://SHOUTCAST-IP:PORT/admin.cgi

我需要登录并从http://SHOUTCAST-IP:PORT/admin.cgi?mode=viewxml获取 XML 数据并使用 php 进行操作,我已经制作了这个脚本

$fp = fsockopen($server, $port, $errno, $errstr, 30);
      fputs($fp, "GET /admin.cgi?pass=".$password."&mode=viewxml HTTP/1.0\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)\r\n\r\n"); //
      while (!feof($fp)) {
          $content = fgets($fp);
}

但它不起作用,它说Unauthorised。我该如何解决?

4

2 回答 2

0

那这个呢?

//*********** YOUR FM ***********//
// Begin Configuration
$scdef  =       "YOUR FM"; 
                                        // ABOVE: Default station name to display when server or stream is down
$scip   =       "127.0.0.1";        // ip or url of shoutcast server (DO NOT ADD HTTP:// don't include the port)
$scport =       "5977";              // port of shoutcast server
$scpass =       "mysecretpassword";              // password to shoutcast server

// End Configuration
//*********** YOUR FM ***********//

$scfp = fsockopen("$scip", $scport, &$errno, &$errstr, 30);
 if(!$scfp) {
  $scsuccs=1;
echo''.$scdef.' is Offline';
 }
if($scsuccs!=1){
 fputs($scfp,"GET /admin.cgi?pass=$scpass&mode=viewxml HTTP/1.0\r\nUser-Agent: SHOUTcast Song Status (Mozilla Compatible)\r\n\r\n");
 while(!feof($scfp)) {
  $page .= fgets($scfp, 1000);
 }

 // REST OF YOUR CODE BELOW HERE!
于 2014-06-27T03:14:55.380 回答
0

你确定你可以使用标准的 $_GET 变量来提供usernamepassword?似乎很糟糕的做法!?

我会使用 cURL 来实现这一点,它既快速又简单!

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://SHOUTCAST-IP:PORT/admin.cgi");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "user:pwd");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
$output = curl_exec($ch);
curl_close($ch);
于 2012-07-17T12:55:30.460 回答