PHP $this-> 没有正确设置变量
PHP $this-> is not properly setting a variable
编辑2:
完整的工作代码
<?php
include_once('config.php');
function getAccountCredentials(){
//here we are getting our account credentias from the accountConfiguration class
$accountCredentials = new accountConfiguration;
$account_id = $accountCredentials->account_id;
$api_accesskey = $accountCredentials->api_accesskey;
$accountArray = array('account_id'=>$account_id, 'api_accesskey'=>$api_accesskey);
$transactionRequestParameters = json_encode(array('data'=>$accountArray), JSON_PRETTY_PRINT);
$decode = json_decode($transactionRequestParameters, true, 4);
$credentials = array_shift($decode);
return $credentials;
}
function getBaseURL($val){
$baseURL = new baseURL;
$url = $baseURL->url;
return $url.$val;
}
class apiRequest{
private $url;
private $params = array();
public $response = array();
public function setUrl($val) {
$baseUrl = getBaseURL($val);
$this->url = $baseUrl;
}
public function setAccountCredentials(){
//This calls our account credentials function.
$account_info = getAccountCredentials();
//Here We're setting our account information to variables.
$account_id=$account_info['account_id'];
$api_accesskey = $account_info['api_accesskey'];
//Here I am setting the account information to the params variable.
$this->setParam('account_id', $account_id);
$this->setParam('api_accesskey', $api_accesskey);
}
public function setParam($pname, $value) {
$this->params[$pname] = $value;
}
public function setParams($params) {
if (is_array($params)) {
$this->params = array_merge($this->params, $params);
} else {
die('Must send array');
}
}
public function sendRequest(){
$pdata = $this->params;
$url = $this->url;
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pdata);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$res = curl_exec($ch);
$response_decode = json_decode($res, true);
var_export($this->response = $response_decode);
}
public function ReportingServices($request_parameters){
$api_url = "REDACTED";
$request = new apiRequest;
$request->setAccountCredentials();
$request->setUrl($api_url);
$request_information = array('response_format'=>'JSON');
$request ->setParams($request_information);
$request ->setParams($request_parameters);
$request->sendRequest();
}
}
$request = new apiRequest;
$values = array ('start_date'=>'2015-04-15','end_date'=>'2015-04-18');
$request->ReportingServices($values);
?>
我想要完成的事情:
现在,如果我将这个函数添加到我的方法中。
public function getResponse(){
return $this->response;
var_export($this->response);
}
然后编辑 sendRequest()
以执行以下操作:
public function sendRequest(){
$pdata = $this->params;
$url = $this->url;
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pdata);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$res = curl_exec($ch);
$response_decode = json_decode($res, true);
$this->response = $response_decode;
}
然后这样调用我的方法:
$request = new apiRequest;
$values = array ('start_date'=>'2015-04-15','end_date'=>'2015-04-18');
$request->ReportingServices($values);
$response = $request->getResponse();
var_export($response);
$response
上的 var_export
不起作用。
看来你确实重新初始化了class。
$request = new apiRequest;
// ...
public function ReportingServices($request_parameters){
$api_url = "REDACTED";
$request = new apiRequest;
$request->setAccountCredentials();
$request->setUrl($api_url);
$request_information = array('response_format'=>'JSON');
$request ->setParams($request_information);
$request ->setParams($request_parameters);
$request->sendRequest();
}
两个实例不一样。他们不共享任何变量。您在一个实例中设置变量并尝试在另一个实例中获取它,但这是行不通的。
我看到的一个问题是您从未正确设置 $params
。正如所写,您的 sendRequest()
函数不接受任何参数。但是,它使用 $this->params
作为数据负载。这将始终为空 array()
。所以.. 这给你留下了三个选项之一:
- 确保
sendRequest()
确实接受了 args,并放弃使用 $this->params
- 为
$this->params
创建一个 setter 并在 sendRequest()
之前调用它
- 使用构造函数 (
__contruct
),当您创建 apiRequest
时将设置 $this->params
编辑2: 完整的工作代码
<?php
include_once('config.php');
function getAccountCredentials(){
//here we are getting our account credentias from the accountConfiguration class
$accountCredentials = new accountConfiguration;
$account_id = $accountCredentials->account_id;
$api_accesskey = $accountCredentials->api_accesskey;
$accountArray = array('account_id'=>$account_id, 'api_accesskey'=>$api_accesskey);
$transactionRequestParameters = json_encode(array('data'=>$accountArray), JSON_PRETTY_PRINT);
$decode = json_decode($transactionRequestParameters, true, 4);
$credentials = array_shift($decode);
return $credentials;
}
function getBaseURL($val){
$baseURL = new baseURL;
$url = $baseURL->url;
return $url.$val;
}
class apiRequest{
private $url;
private $params = array();
public $response = array();
public function setUrl($val) {
$baseUrl = getBaseURL($val);
$this->url = $baseUrl;
}
public function setAccountCredentials(){
//This calls our account credentials function.
$account_info = getAccountCredentials();
//Here We're setting our account information to variables.
$account_id=$account_info['account_id'];
$api_accesskey = $account_info['api_accesskey'];
//Here I am setting the account information to the params variable.
$this->setParam('account_id', $account_id);
$this->setParam('api_accesskey', $api_accesskey);
}
public function setParam($pname, $value) {
$this->params[$pname] = $value;
}
public function setParams($params) {
if (is_array($params)) {
$this->params = array_merge($this->params, $params);
} else {
die('Must send array');
}
}
public function sendRequest(){
$pdata = $this->params;
$url = $this->url;
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pdata);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$res = curl_exec($ch);
$response_decode = json_decode($res, true);
var_export($this->response = $response_decode);
}
public function ReportingServices($request_parameters){
$api_url = "REDACTED";
$request = new apiRequest;
$request->setAccountCredentials();
$request->setUrl($api_url);
$request_information = array('response_format'=>'JSON');
$request ->setParams($request_information);
$request ->setParams($request_parameters);
$request->sendRequest();
}
}
$request = new apiRequest;
$values = array ('start_date'=>'2015-04-15','end_date'=>'2015-04-18');
$request->ReportingServices($values);
?>
我想要完成的事情:
现在,如果我将这个函数添加到我的方法中。
public function getResponse(){
return $this->response;
var_export($this->response);
}
然后编辑 sendRequest()
以执行以下操作:
public function sendRequest(){
$pdata = $this->params;
$url = $this->url;
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pdata);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$res = curl_exec($ch);
$response_decode = json_decode($res, true);
$this->response = $response_decode;
}
然后这样调用我的方法:
$request = new apiRequest;
$values = array ('start_date'=>'2015-04-15','end_date'=>'2015-04-18');
$request->ReportingServices($values);
$response = $request->getResponse();
var_export($response);
$response
上的 var_export
不起作用。
看来你确实重新初始化了class。
$request = new apiRequest;
// ...
public function ReportingServices($request_parameters){
$api_url = "REDACTED";
$request = new apiRequest;
$request->setAccountCredentials();
$request->setUrl($api_url);
$request_information = array('response_format'=>'JSON');
$request ->setParams($request_information);
$request ->setParams($request_parameters);
$request->sendRequest();
}
两个实例不一样。他们不共享任何变量。您在一个实例中设置变量并尝试在另一个实例中获取它,但这是行不通的。
我看到的一个问题是您从未正确设置 $params
。正如所写,您的 sendRequest()
函数不接受任何参数。但是,它使用 $this->params
作为数据负载。这将始终为空 array()
。所以.. 这给你留下了三个选项之一:
- 确保
sendRequest()
确实接受了 args,并放弃使用$this->params
- 为
$this->params
创建一个 setter 并在sendRequest()
之前调用它
- 使用构造函数 (
__contruct
),当您创建apiRequest
时将设置
$this->params