我在Bluemix中托管的docker容器上运行PHP代码。 PHP代码调用python脚本,该脚本是基于MQTT的订阅代码。我的想法是,每次订阅的代码获得MQTT消息时,它都会将值写入文本文件。 PHP代码将每隔10秒检查一次文件中的新值。 VCAP_ENV变量正在正确写入。但是,该网站无法加载。
当我在本地尝试时,python脚本成功执行。所以也没有错误。 我的代码如下:
PHP代码:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Database Name',
'USER': 'Database Username',
'PASSWORD': 'Database Password',
'HOST': 'localhost',
'PORT': '3306',
}
}
Python脚本:
<?php
if( getenv("VCAP_SERVICES") ) {
// get IoT service configuration from Bluemix
$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services, true);
$mysql_config = $services_json["iotf-service"][0]["credentials"];
$org_id = $mysql_config["org"];
$port = $mysql_config["mqtt_u_port"];
$username = $mysql_config["apiKey"];
$password = $mysql_config["apiToken"];
}
// set configuration values
$config = array(
'org_id' => $org_id,
'port' => $port,
'app_id' => 'mymqttfinalservice',
'iotf_api_key' => $username,
'iotf_api_secret' => $password,
'device_id' => '007',
'qos' => 1
);
$file = fopen("VCAP_CONFIG.ini","w");
#fwrite($file,"[config]" . PHP_EOL );
#fwrite($file,"org =" . $org_id . PHP_EOL );
#fwrite($file,"apikey =" . $username . PHP_EOL );
#fwrite($file,"authkey =" . $password . PHP_EOL );
fwrite($file,"[config]" . "\n" );
fwrite($file,"org =" . $org_id . "\n" );
fwrite($file,"apikey =" . $username . "\n" );
fwrite($file,"authkey =" . $password . "\n" );
fclose($file);
$file = file_get_contents('VCAP_CONFIG.ini', true);
echo $file;
$command = 'chmod 777 /app/PythonSubscribeCode.py';
$output = '';
exec ( $command);
$command = 'python3 /app/PythonSubscribeCode.py 2>&1';
$output = exec ($command);
print_r($output);
$x = 1;
while($x == 1)
{
$config = parse_ini_file('Data.ini');
echo json_encode($config);
sleep(5);
}
?>
有人可以指导吗?
答案 0 :(得分:0)
您是否从cf ic logs containerid
收到任何有用的错误消息,以查看可能失败的内容?作为替代方案,您还可以尝试执行容器(cf ic exec -ti containerid bash
或docker exec...
)并直接运行python脚本以查看在运行时是否会给您带来其他错误。