您好,我正在使用 Magento Soap clinet。我正在一个控制器内创建很多功能,这就是为什么我想将 $client 和 $session_id 设置为全局。
这是我的代码-
<?php
//关闭所有错误报告
error_reporting(0);
//包括 SOAP 客户端
require_once APPPATH.'third_party/client/soap_clinet.php';
//API 类
class Api extends CI_Controller{
//公共变量
public $variable = "tree"; //working fine
//如下定义$client对象也会产生错误。
public $client = new SoapClient('http://localhost/mystore/index.php/api/?wsdl');
public $session_id;
public function _construct()
{
parent::_construct();
}
function index()
{
$data['title'] = "SOAP";
$data['heading'] = "Showing Magento SOAP connectiviy";
$this->load->view('apiview', $data);
}
//我需要此功能的帮助
function login(){
try{
$this->session_id = $this->client->login(
'fmniloy',
'abc123'
);
echo 'Connection complete: session id ='.$this->session_id;
}
catch (SoapFault $fault)
{
echo 'Fault Code: '.$fault->faultcode.'<br/>';
echo 'Fault Reason: '.$fault->faultstring;
}
} //login ends
function tree()
{
//it's printing global $variable successfully
echo $this->variable;
}
}
?>
终于我找到了解决方案。 首先,不可能将对象声明为公共对象。我们可以将 $client 对象和 $session_id 传递给其他函数。解决办法如下。
<?php
//关闭所有错误报告 错误报告(0); //包括 SOAP 客户端
require_once APPPATH.'third_party/client/soap_clinet.php';
//API 类
class Api extends CI_Controller{
//public variable
public $remote_server = "http://localhost/mystore/index.php/api/?wsdl";
public $username = "fmniloy";
public $password = "abc123";
public function _construct()
{
parent::_construct();
}
function index()
{
$data['title'] = "SOAP";
$data['heading'] = "Showing Magento SOAP connectiviy";
$this->load->view('apiview', $data);
}
//Login to Server
function login(){
//login to Client Serve
$client = new SoapClient($this->remote_server);
try{
//filled with webservice username and passwd
$session_id = $client->login(
$this->username,
$this->password
);
//echo 'Connection complete: session id ='. $session_id;
}
catch (SoapFault $fault)
{
echo 'Fault Code: '.$fault->faultcode.'<br/>';
echo 'Fault Reason: '.$fault->faultstring;
}
//将$client对象和$session_id输入到数组中 $soap_vars = array($client, $session_id); //返回值供其他函数使用 返回 $soap_vars;
}
//登录结束
function tree()
{
$soap_vars = $this->login();
$result = $soap_vars[0]->call($soap_vars[1], 'catalog_category.tree');
echo '</br></br></br>Catagory Tree: ';
var_dump($result);
}
}
?>