动态子域创建时fgets()响应null

时间:2016-06-04 11:28:41

标签: php subdomain

我想创建动态子域。当我回显fputs()时,它显示一些数字,这意味着它的工作,但当我们使用fgets()检索数据时,获取null。这是我的代码。

// your cPanel username
$cpanel_user = 'username';

// your cPanel password
$cpanel_pass = 'password';

// your cPanel skin
$cpanel_skin = 'x';

// your cPanel domain
$cpanel_host = 'domain.com';

// subdomain name
$subdomain = 'mysubdomain';

// create the subdomain

$sock = fsockopen($cpanel_host,80, $errno, $errstr, 60);
if(!$sock) {
echo "ERROR: $errno - $errstr<br />\n";
  print('Socket error');
  exit();
}
echo $sock."<br>";
$pass = base64_encode("$cpanel_user:$cpanel_pass");
echo "$cpanel_user:$cpanel_pass<br>";
$in = "GET /frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$cpanel_host&domain=$subdomain\r\n";
$in .= "HTTP/1.0\r\n";
$in .= "Host:$cpanel_host\r\n";
$in .= "Authorization: Basic $pass\r\n";
$in .= "\r\n";
echo $in."<br>";
$result="";
fputs($sock, $in);
while (!feof($sock)) {
  $result .= fgets ($sock);
}
fclose($sock);

echo "Result:- ".$result;

我没有得到我错的地方。

1 个答案:

答案 0 :(得分:1)

您的HTTP格式不正确。 GET行应该是:

GET <pathname> <HTTPversion>

但你要发送

GET <pathname>
<HTTPversion>

更改这些行:

$in = "GET /frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$cpanel_host&domain=$subdomain\r\n";
$in .= "HTTP/1.0\r\n";

$in = "GET /frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$cpanel_host&domain=$subdomain HTTP/1.0\r\n";

你也不应该使用

while (!feof($sock))

PHP - while loop (!feof()) isn't outputting/showing everything。相反,使用:

while ($line = fgets($sock)) {
    $result .= $line;
}

但您可以使用file_get_contents

简化所有这些操作
$result = file_get_contents("http://$cpanel_user:$cpanel_pass@$cpanel_host/frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$cpanel_host&domain=$subdomain");
相关问题