PHP $ this->没有正确设置变量

时间:2015-04-17 19:41:05

标签: php oop curl methods this

EDIT2: 完整的工作代码

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

var_export上的$response不起作用。

2 个答案:

答案 0 :(得分:0)

看来你确实重新初始化了这门课程。

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

这两个实例并不相同。他们不共享任何变量。您在一个实例中设置变量并尝试将其放入另一个实例中,但这不起作用。

答案 1 :(得分:0)

我看到的一个问题是你永远不会正确设置$params。如上所述,您的sendRequest()函数不带任何参数。但是,它使用$this->params作为其数据有效负载。这将始终为空array()。所以..让你有三个选择之一:

  1. 确保sendRequest()实际采用args,并使用$this->params
  2. $this->params创建一个setter,并在sendRequest()
  3. 之前调用它
  4. 使用构建函数(__contruct),在创建$this->params时设置apiRequest