php xmlrpc阅读

时间:2012-03-24 10:54:50

标签: php xml-rpc openerp

我不会从表中读取行以将主题放入列表中 php scripte

$url_obj='http://'.$host.':8069/xmlrpc/object';
$sock=new xmlrpc_client($url_obj);
$msg=new xmlrpcmsg('execute');
$arrVal=array('user_id', 'name');
$msg->addParam(new xmlrpcval($db, "string"));
$msg->addParam(new xmlrpcval(1, "string"));
$msg->addParam(new xmlrpcval($pwd, "string"));
$msg->addParam(new xmlrpcval("res.partner", "string"));
$msg->addParam(new xmlrpcval("read", "string"));
$msg->addParam(new xmlrpcval(3, "int"));
//$msg->addParam(new xmlrpcval($arrVal, "struct"));
print_r("message msg=");
print_r($msg);
$resp= $sock->send($msg);
$val=$resp->value();
$read=$val->scalarval();
echo $read;

我收到错误PHP致命错误:在非对象上调用成员函数scalarval()

message msg=xmlrpcmsg Object
(
    [payload] =>
    [methodname] => execute
    [params] => Array
    (
        [0] => xmlrpcval Object
            (
                [me] => Array
                    (
                        [string] => database
                    )

                [mytype] => 1
                [_php_class] =>
            )

        [1] => xmlrpcval Object
            (
                [me] => Array
                    (
                        [string] => 1
                    )

                [mytype] => 1
                [_php_class] =>
            )

        [2] => xmlrpcval Object
            (
                [me] => Array
                    (
                        [string] => ******
                    )

                [mytype] => 1
                [_php_class] =>
            )

        [3] => xmlrpcval Object
            (
                [me] => Array
                    (
                        [string] => res.partner
                    )

                [mytype] => 1
                [_php_class] =>
            )

        [4] => xmlrpcval Object
            (
                [me] => Array
                    (
                        [string] => read
                    )

                [mytype] => 1
                [_php_class] =>
            )

        [5] => xmlrpcval Object
            (
                [me] => Array
                    (
                        [int] => 3
                    )

                [mytype] => 1
                [_php_class] =>
            )

    )

    [debug] => 0
    [content_type] => text/xml
)
PHP Fatal error:  Call to a member function scalarval() on a non-object in t./est.php on line 27

PHP致命错误:在第27行的t./est.php中调用非对象的成员函数scalarval()!

你可以帮帮我吗?

3 个答案:

答案 0 :(得分:2)

<?php

include_once("lib/xmlrpc.inc");

class OpenERPXmlrpc {

   private $user, $password, $database, $services, $client, $res, $msg, $id;

   function __construct($usr, $pass, $db, $server) {

      $this->user = $usr;
      $this->password = $pass;
      $this->database = $db;
      $this->services = $server;

      $this->client = new xmlrpc_client($this->services.'common');

      $this->msg = new xmlrpcmsg('login');
      $this->msg->addParam(new xmlrpcval($this->database, "string"));
      $this->msg->addParam(new xmlrpcval($this->user, "string"));   
      $this->msg->addParam(new xmlrpcval($this->password, "string"));

      $this->res =  &$this->client->send($this->msg);

      if(!$this->res->faultCode()){

         $this->id = $this->res->value()->scalarval();

      }
      else {

         echo "Unable to login ".$this->res->faultString();
         exit;
      }
   }

   function read($post = null) {

      $this->client = new xmlrpc_client($this->services.'object');

      if(empty($post)) {

         $ids_read = array(new xmlrpcval('1', 'int'), new xmlrpcval('2', 'int'));

         $key = array(new xmlrpcval('id','integer') , new xmlrpcval('name', 'string'));

         $this->msg = new xmlrpcmsg('execute');
         $this->msg->addParam(new xmlrpcval($this->database, "string"));
         $this->msg->addParam(new xmlrpcval(1, "int"));
         $this->msg->addParam(new xmlrpcval($this->password, "string"));
         $this->msg->addParam(new xmlrpcval("res.partner","string"));
         $this->msg->addParam(new xmlrpcval("read", "string"));
         $this->msg->addParam(new xmlrpcval($ids_read, "array"));
         $this->msg->addParam(new xmlrpcval($key, "array"));

         $this->res = &$this->client->send($this->msg);

         if(!$this->res->faultCode()) {

            $read_html = '<table width="20%" border="0" cellpadding="4" cellspacing="4" align="center">
                        <tr>
                           <th>Id</th>
                           <th>Name</th>
                        </tr>
                     ';

            $scalval = $this->res->value()->scalarval();

            foreach ($scalval as $keys => $values) {

               $value = $values->scalarval();

               $read_html .= '
                     <tr>
                        <td>'.$value['id']->scalarval().'</td>
                        <td>'.$value['name']->scalarval().'</td>
                     </tr>
                     <tr><td colspan="2"></td></tr>
                        ';
            }

            $read_html .= '
                     </table>
                     ';

            return $read_html;
         }
         else {
            return "Not read recode from partner table <br />".$this->res->faultString();
         }
      }
   }

}

$cnt = new OpenERPXmlrpc('admin', 'a', 'test_6_1', 'http://localhost:8069/xmlrpc/');

echo $cnt->read();

?>

解决;)

答案 1 :(得分:1)

看起来你在这里有几个问题...... 至于这一行:

$msg->addParam(new xmlrpcval(3, "int"));

您需要提供要阅读的ID列表。在你的情况下,你有ID = 3买你还需要把它全部包装成xml proc变量;类似的东西:

$msg->addParam(new xmlrpcval(array(new xmlrpcval(3, "int")), "array"));

顺便说一句,您是否尝试过通过xml rpc登录以确保连接正常?

类似的东西:

$sock = new xmlrpc_client($server_url.'common'); 
$msg = new xmlrpcmsg('login'); 
$msg->addParam(new xmlrpcval($dbname, "string")); 
$msg->addParam(new xmlrpcval($user, "string")); 
$msg->addParam(new xmlrpcval($password, "string")); 
$resp = $sock->send($msg); 
$val = $resp->value(); 
$id = $val->scalarval(); 

干杯,

伊维

答案 2 :(得分:0)

也许这也有帮助。

我编写了自己的函数来添加一些参数。像这样使用它:

预定义:$this->m_objXmlRpc_Message已在课程中启动。 将函数放在一个类中。

$this->addParam($p_sSessionID); //String
$this->addParam($p_aUserParams, "struct"); //Ass. Array

//把它放在课堂上或重写函数....

protected function addParam($p_xParam,$p_sType="string")
{
    //check type, default is string
    //if type is wrong, then it will be string
    switch($p_sType)
    {
        case "string":
        case "int":
        case "boolean":
        case "double":
        case "dateTime.iso8601":
        case "base64":
            break;
        case "array":
        case "struct":
            $p_xParam = $this->createArray($p_xParam);
            break;
        default: $p_sType = "string";
    }
    $param = new xmlrpcval($p_xParam, $p_sType);
    $this->m_objXmlRpc_Message->addParam($param);
}

protected function createArray($p_xParam)
{
    foreach($p_xParam as $key => $value)
    {
        $p_xParam[$key] = new xmlrpcval($value); 
    }
    return $p_xParam;
}