Xmlrpc,php 操作 OE 的方法 --- 搜索、读取、创建、更新
-
php 操作 OE 的方法
phpxmlrpc 版本:v2.2.2( http://phpxmlrpc.sourceforge.net/ )
openerp 版本:v7.0-20130103-002220-1
LNMP 版本:v0.9
* 搜索
* 读取
* 创建
* 更新。
如果报 "Call to a member function scalarval() on a non-object" 错,请看看是不是 fsockopen 函数被禁用。
* vi /usr/local/php/etc/php.ini
* 搜索 disable_functions, 删除里面的 fsockopen
还有个问题,创建和更新的方法,不能用 $GLOBALS['xmlrpc_internalencoding']='UTF-8'; 必须要用 ISO-8859-1,否则就会报错。
<br /><?php<br />require("./xmlrpc/lib/xmlrpc.inc");<br />// 这里就是解决商品名称中有中文时的乱码问题<br />//$GLOBALS['xmlrpc_internalencoding']='UTF-8'; // 如果从里定义的话,那么 create、write 方法就会出现 500 错误。<br />class XmlrpcOe{<br /> public $user = 'admin';<br /> public $password = 'admin';<br /> public $user_id = -1;<br /> public $dbname = 'test';<br /> public $server_url = 'http://127.0.0.1:8069/xmlrpc/';<br /> public function __construct() {<br /> $this->sock_common();<br /> $this->sock_object();<br /> $this->connect();<br /> }<br /><br /> public function sock_common() {<br /> $this->sock_com = new xmlrpc_client($this->server_url.'common');<br /> }<br /> public function sock_object() {<br /> $this->sock_obj = new xmlrpc_client($this->server_url.'object');<br /> }<br /> /**<br /> * 登录,获取 user_id<br /> */<br /> public function connect() {<br /> /*<br /> if(isset($_COOKIE["user_id"]) == true) {<br /> if($_COOKIE["user_id"]>0) {<br /> return $_COOKIE["user_id"];<br /> }<br /> }<br /> */<br /> $msg = new xmlrpcmsg('login');<br /> $msg->addParam(new xmlrpcval($this->dbname, "string"));<br /> $msg->addParam(new xmlrpcval($this->user, "string"));<br /> $msg->addParam(new xmlrpcval($this->password, "string"));<br /> $resp = $this->sock_com->send($msg);<br /> $val = $resp->value();<br /> $this->user_id = $val->scalarval();<br /> setcookie("user_id",$this->user_id,time()+3600);<br /> }<br /><br /> /**<br /> * 搜索-单条件<br /> * $relation: 搜索的对象,如:product.category<br /> * $attribute: 属性,如:name<br /> * $operator: 条件,如:>、=、!=、like<br /> * $keys: 值,如:p<br /> */<br /> public function search($relation, $attribute, $operator, $keys) {<br /> $GLOBALS['xmlrpc_internalencoding']='UTF-8';<br /> $key = array(new xmlrpcval(array(new xmlrpcval($attribute , "string"),<br /> new xmlrpcval($operator,"string"),<br /> new xmlrpcval($keys,"string")),"array"),);<br /> $msg = new xmlrpcmsg('execute');<br /> $msg->addParam(new xmlrpcval($this->dbname, "string"));<br /> $msg->addParam(new xmlrpcval($this->user_id, "int"));<br /> $msg->addParam(new xmlrpcval($this->password, "string"));<br /> $msg->addParam(new xmlrpcval($relation, "string"));<br /> $msg->addParam(new xmlrpcval("search", "string"));<br /> $msg->addParam(new xmlrpcval($key, "array"));<br /> $resp = $this->sock_obj->send($msg);<br /> $val = $resp->value();<br /> $ids_rpc_array = $val->scalarval();<br /> $ids = array();<br /> foreach($ids_rpc_array as $id_obj) {<br /> $ids[] = $id_obj->me["int"];<br /> }<br /> return $ids_rpc_array;<br /> //return $ids;<br /> }<br /> /**<br /> * 读数据,通过 id 读取相关数据<br /> * $relation: 数据对象,如:product.product<br /> * $ids: id 的数组,如:array(new xmlrpcval(4, "int"));<br /> * $field_array: 字段数组<br /> */<br /> public function read($relation, $ids, $field_array) {<br /> $GLOBALS['xmlrpc_internalencoding']='UTF-8';<br /> <br /> $msg = new xmlrpcmsg('execute');<br /> $msg->addParam(new xmlrpcval($this->dbname, "string"));<br /> $msg->addParam(new xmlrpcval($this->user_id, "int"));<br /> $msg->addParam(new xmlrpcval($this->password, "string"));<br /> $msg->addParam(new xmlrpcval($relation, "string"));<br /> $msg->addParam(new xmlrpcval("read", "string"));<br /> $msg->addParam(new xmlrpcval($ids, "array"));<br /> $msg->addParam(new xmlrpcval($field_array, "array"));<br /> $resp = $this->sock_obj->send($msg);<br /> $val = $resp->value();<br /> return $val->scalarval();<br /> }<br /> public function create($relation, $partner_data) {<br /> $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';<br /> $msg = new xmlrpcmsg('execute');<br /> $msg->addParam(new xmlrpcval($this->dbname, "string"));<br /> $msg->addParam(new xmlrpcval($this->user_id, "int"));<br /> $msg->addParam(new xmlrpcval($this->password, "string"));<br /> $msg->addParam(new xmlrpcval($relation, "string"));<br /> $msg->addParam(new xmlrpcval("create", "string"));<br /> $msg->addParam(new xmlrpcval($partner_data, "struct"));<br /> $resp = $this->sock_obj->send($msg);<br /> if ($resp->faultCode())<br /> echo 'Error: '.$resp->faultString();<br /> else<br /> echo 'Partner '.$resp->value()->scalarval().' created !';<br /> }<br /> public function write($relation, $data, $ids) {<br /> $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';<br /> $msg = new xmlrpcmsg('execute');<br /> $msg->addParam(new xmlrpcval($this->dbname, "string"));<br /> $msg->addParam(new xmlrpcval($this->user_id, "int"));<br /> $msg->addParam(new xmlrpcval($this->password, "string"));<br /> $msg->addParam(new xmlrpcval($relation, "string"));<br /> $msg->addParam(new xmlrpcval("write", "string"));<br /> $msg->addParam(new xmlrpcval($ids, "array"));<br /> $msg->addParam(new xmlrpcval($data, "struct"));<br /> $resp = $this->sock_obj->send($msg);<br /> $val = $resp->value();<br /> return $val->scalarval();<br /> }<br />}<br />$xmlrpc_oe = new XmlrpcOe();<br /># 搜索<br />echo "search:<br />";<br />$ids = $xmlrpc_oe->search("product.product", "id", ">", "1");<br />print_r($ids);<br />echo "<br /><br />";<br /><br />$id_array = array();<br />foreach($ids as $id_obj) {<br /> $id_array[] = $id_obj->me["int"];<br />}<br />print_r($id_array);<br />echo "<br /><br />---------------------------------------------------------<br />";<br /> <br /># 读数据<br />echo "read:<br />";<br />$field_array = array(<br /> new xmlrpcval("id", "int"),<br /> new xmlrpcval("name", "string"),<br /> new xmlrpcval("list_price", "string"),<br /> );<br />$ret_array = $xmlrpc_oe->read("product.product", $ids, $field_array);<br />print_r($ret_array);<br />echo "<br /><br />";<br />$out_array = array();<br />foreach($ret_array as $k => $v) {<br /> $tmp_id = $v->me['struct']['id']->me["int"];<br /> $tmp_name = $v->me['struct']['name']->me["string"];<br /> $tmp_price = $v->me['struct']['list_price']->me["double"];<br /> $out_array[] = array(<br /> "id" => $tmp_id,<br /> "name" => $tmp_name,<br /> "list_price" => $tmp_price,<br /> );<br />}<br />print_r($out_array);<br />echo "<br /><br />---------------------------------------------------------<br />";<br /><br /># 添加<br />echo "create:<br />";<br />$partner_data = array(<br /> "name" => new xmlrpcval("魅族 MX2 32GB", "string"),<br /> "list_price" => new xmlrpcval(2999.00, "double"),<br /> );<br />//$xmlrpc_oe->create("product.product", $partner_data);<br />echo "<br /><br />---------------------------------------------------------<br />";<br /><br /># 修改<br />echo "write:<br />";<br />$ids = array(<br /> new xmlrpcval(7, "int"),<br /> );<br />$data = array(<br /> "name" => new xmlrpcval("魅族 MX2 64GB", "string"),<br /> "list_price" => new xmlrpcval(3999.00, "double"),<br /> );<br />$xmlrpc_oe->write("product.product", $data, $ids);<br />$ret_array = $xmlrpc_oe->read("product.product", $ids, $field_array);<br />$out_array = array();<br />foreach($ret_array as $k => $v) {<br /> $tmp_id = $v->me['struct']['id']->me["int"];<br /> $tmp_name = $v->me['struct']['name']->me["string"];<br /> $tmp_price = $v->me['struct']['list_price']->me["double"];<br /> $out_array[] = array(<br /> "id" => $tmp_id,<br /> "name" => $tmp_name,<br /> "list_price" => $tmp_price,<br /> );<br />}<br />print_r($out_array);<br /><br />
参考:
[检测到链接无效,已移除]
http://doc.openerp.com/v6.0/developer/6_22_XML-RPC_web_services/index.html