2012-03-24 57 views
2

我不會讀,從錶行把主題列表中的 PHP scriptePHP的XML-RPC讀

$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致命錯誤:調用一個成員函數scalarval()一個非對象在t./est.php第27行!

你能幫助我嗎?

+2

你從服務器獲取有何反應?試試這個:'print_r($ val);'在調用'scalarval()'之前。 – 2012-03-27 21:05:10

回答

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

你介意告訴我們究竟發生了什麼變化,所以我們不必運行'diff'來查找? – SomeKittens 2012-05-21 13:52:53

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(); 

乾杯,

伊維察

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; 
}