gpt4 book ai didi

php - 如何让我的 PHP 代码打印到单独的 div?

转载 作者:行者123 更新时间:2023-11-28 17:11:16 27 4
gpt4 key购买 nike

我正在尝试将此 php 代码的打印输出到单独的 html div 中,以便我可以使用 css 自定义它们。我制作了一个 css 文件,它显示了,但所有内容都打印到一个名为 body class="hasGoogleVoiceExt"的 div 中。

<?php
class Bot {
public $botHost = '127.0.0.1';
public $botPort = 8087;
public $botId = NULL;
public $token = NULL;
public function __construct($botHost = '127.0.0.1', $botPort = 8087, $botId = NULL) {
$this->botHost = $botHost;
$this->botPort = $botPort;
if ($botId == NULL) {
$botId = $this->DefaultBot();
}
$this->botId = $botId;
}
public function DefaultBot() {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://'.$this->botHost.':'.$this->botPort.'/api/v1/botId',
CURLOPT_RETURNTRANSFER => 1
));
$data = curl_exec($ch);
curl_close($ch);
$json = json_decode($data, TRUE);
return $json['defaultBotId'];
}
public function Login($username, $password) {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://'.$this->botHost.':'.$this->botPort.'/api/v1/bot/login',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => json_encode(array('username' => $username, 'password' => $password, 'botId' => $this->botId)),
CURLOPT_HTTPHEADER => array('Content-Type: application/json')
));
$data = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code != 200) return NULL;
$this->token = json_decode($data, TRUE)['token'];
}
public function GetInstances() {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://'.$this->botHost.':'.$this->botPort.'/api/v1/bot/instances',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array('Authorization: bearer '.$this->token)
));
$data = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code != 200) return NULL;
$json = json_decode($data, TRUE);
return $json;
}
public function GetInstanceStatus($instanceId) {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://'.$this->botHost.':'.$this->botPort.'/api/v1/bot/i/'.$instanceId.'/status',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array('Authorization: bearer '.$this->token)
));
$data = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code != 200) return NULL;
$json = json_decode($data, TRUE);
return $json;
}
}

$bot = new Bot('127.0.0.1', 8087);
$bot->Login('admin', 'foobar');
$instances = $bot->GetInstances();
for ($i = 0; $i < count($instances); $i++) {
$info = $bot->GetInstanceStatus($instances[$i]['uuid']);
if ($info['currentTrack'] != NULL && $info['playing']) {
printf("%s is playing %s by %s\n", $instances[$i]['nick'], $info['currentTrack']['title'], $info['currentTrack']['artist']);
} else {
printf("%s is not playing anything right now\n", $instances[$i]['nick']);
}
echo '<link href="botcss/styles.css" rel="stylesheet" type="text/css" />';
}

我目前正在这里测试 http://theunlighted.com/nowplaying.php

最佳答案

要事第一:你的 <link [...]>需要在for()之前输出循环。

其次,以某种方式(我认为您打算这样做)输出 div 很简单:

for($i = 0; $i < 123; $i++) {
echo '<div class="foo foo_'.$i.'">';
// do other output here.
echo '</div>';
}

关于php - 如何让我的 PHP 代码打印到单独的 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29157368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com