我正在尝试创建一个代表NXCP消息的类(它与监控系统NetXMS一起使用的协议),我试图对其进行修改http://wiki.netxms.org/wiki/Communication_Protocol_Reference_Guide
..到目前为止我的代码
NXCP.inc.php:
<?php
class message_nxcp {
public $message;
public $header;
public $payload;
public $padding;
function header ($name,$ID,$eof,$de,$ro,$eos,$c )
{ //CODE
$sql= "select value from nxcp_code where name = '$name'" ;
$code=mysql_query($sql);
//FLAG
//$sql1="select Value from flag where name = '$flag'"; $flag=mysql_query($sql1);
if ($eof=true){ $flag =0x0002;}
elseif ($de=true) {$flag = 0x0004;}
elseif ($eos=true) {$flag = 0x0008;}
elseif ($ro=true) {$flag = 0x0010;}
elseif ($c=true) {$flag = 0x0020;
$payload= ' ';}
//SIZE
$size=strlen($message);
//ID
$this->id= $ID;
// HEADER
$header= $code.$flag.$size.$id.$df_count;
}
function payload ($var_id,$d_type,$value)
{ //VARIABLE id
$sql= "select value from nxcp_code where name = '$var_id'" ;
$vid=mysql_query($sql);
//data type ,padding ,data
if ($d_type = 'integer')
{ $data_type =0;
$padding1= 00;
$data=$value;
}
if ($d_type ='string' )
{ $data_type =1;
$padding1= 00;
$data= strlen($value).$value;
}
if ($d_type = 'int64')
{ $data_type =2;
$padding1=00 ;
$data=$value;
}
if ($d_type ='int32')
{ $data_type =3;
$padding1= '';
$data=$value;
}
if ($d_type ='binary')
{ $data_type =4;
$padding1= 00;
$data=strlen($value).$value;
}
if ($d_type ='float')
{ $data_type =5;
$padding1= 00;
$data=$value;
}
$payload=$vid.$data_type.$padding1.$data ;
}
function padding ()
{
$i =0 ;
$pad = 262160 - (strlen($header)+strlen($payload));
if ($pad > 0)
{
for ($i=0 ; $i<$pad ; $i++)
{
$padding= $padding.'0' ;
}
}
}
function message()
{ $message = $header.$payload.$padding;
return $message;
}
}
?>
然后我在new.php中创建了我的类message_nxcp
的新对象:
<?php
require_once('NXCP.inc.php');
$message=new message_nxcp;
$message->header('CMD_LOGIN' ,'1',true,false,false,false,false);
$message->payload('VID_LOGIN_NAME','string','admin');
$message->padding();
$message->message();
echo $message;
?>
任何帮助和提前感谢
答案 0 :(得分:0)
首先我不知道,如何实现NXCP消息类,但我看到了代码中的基本问题。
所有这些和解释在这里:
<?php
class message_nxcp {
//use private keyword (i think you does not want to reach this variables directly from outside of the class
private $message;
private $header;
private $payload;
private $padding;
//
private $id; //somewhere down i saw you use it so declared here
function header ($name,$ID,$eof,$de,$ro,$eos,$c )
{ //CODE
$sql= "select value from nxcp_code where name = '$name'" ;
/* Have you already connected to database with `mysql_connect` function? */
$res=mysql_query($sql);
/* the type of $res is mysqlResult (not a string), you should fetch that like this: */
//we should examine , is there error in the query
if( !$res ) throw new Exception(mysql_error(),mysql_errno());
$codeArray = mysql_fetch_assoc($res); //This fetch the first row of the result to an array with numeric indexes.
//Here you should examine is the result empty
if( !$codeArray ) throw new Exception('Code not found'); //somevhere you should catch it
$code = $codeArray[0];
//FLAG
//$sql1="select Value from flag where name = '$flag'"; $flag=mysql_query($sql1);
if ($eof=true){ $flag =0x0002;}
elseif ($de=true) {$flag = 0x0004;}
elseif ($eos=true) {$flag = 0x0008;}
elseif ($ro=true) {$flag = 0x0010;}
elseif ($c=true) {$flag = 0x0020;
// $this keyword
$this->payload= ' ';}
//SIZE
// $this keyword
$size=strlen($this->message);
//ID
$this->id= $ID;
// HEADER
/* the this keyword missing */
$this->header= $code.$flag.$size.$id.$df_count;
}
function payload ($var_id,$d_type,$value)
{ //VARIABLE id
/* do the same as the previus function*/
$sql= "select value from nxcp_code where name = '$var_id'" ;
$vid=mysql_query($sql);
// see the example in the previus function
//data type ,padding ,data
if ($d_type = 'integer')
{ $data_type =0;
$padding1= 00;
$data=$value;
}
if ($d_type ='string' )
{ $data_type =1;
$padding1= 00;
$data= strlen($value).$value;
}
if ($d_type = 'int64')
{ $data_type =2;
$padding1=00 ;
$data=$value;
}
if ($d_type ='int32')
{ $data_type =3;
$padding1= '';
$data=$value;
}
if ($d_type ='binary')
{ $data_type =4;
$padding1= 00;
$data=strlen($value).$value;
}
if ($d_type ='float')
{ $data_type =5;
$padding1= 00;
$data=$value;
}
// $this keyword missing, in php you must use this to access class variables
$this->payload=$vid.$data_type.$padding1.$data ;
}
function padding ()
{
$i =0 ;
//$this keyword
$pad = 262160 - (strlen($this->header)+strlen($this->payload));
if ($pad > 0)
{
for ($i=0 ; $i<$pad ; $i++)
{
//$this keyword
$this->padding= $this->padding.'0' ;
}
}
}
function message()
{
//$this keyword
$this->message = $this->header.$this->payload.$this->padding;
return $this->message;
}
?>
其他代码存在问题:
<?php
require_once('NXCP.inc.php');
try{
$message=new message_nxcp(); //() missed
$message->header('CMD_LOGIN' ,'1',true,false,false,false,false);
$message->payload('VID_LOGIN_NAME','string','admin');
$message->padding();
echo $message->message(); //echo the message
}catch(Exception $e){ //if we throw exceptions, somewhere here we should catch them
//when you debuging:
print '<pre>'.$e->getMessage()."\n".$e->getTraceAsString().'</pre>';
//in production mode, we dont want to show users the details, just this:
print 'Error while processing the request';
exit;
}
?>
其他一些事情:
使用mysqli_*
而非mysql_*
(mysql_*
将在PHP中弃用)
或者,如果您需要Databasde Access的OOP解决方案,则可以使用PDO
阅读以下页面:
http://php.net/manual/en/language.oop5.php,http://php.net/manual/en/book.mysqli.php,http://php.net/manual/en/language.exceptions.php,http://php.net/manual/en/book.pdo.php