I apologize for the incomprehensible title, because I don’t know how to properly name this problem.

There is a class in which authorization and data acquisition from another site is implemented. When calling the get_histroy method get_histroy we need to get the data from the site, being authorized. If we are not authorized, then we call the auth method, which passes authorization, and then call the get_histroy method get_histroy to get the data.

 class TestClass{ /** * @var string */ private $login = ""; private $password = ""; private $phone = ""; private $url = "https://test.ru/"; private $curl = ""; public $cookie_file = 'cookie.txt'; function __construct($login, $password, $phone) { $this->login = $login; $this->password = $password; $this->phone = $phone; $this->curl = curl_init(); } /** * @name Authorization */ private function auth() { $url_auth = "https://auth.test.ru/auth?retpath=https://mail.test.ru"; curl_setopt($this->curl, CURLOPT_POST, 1); $post_login = "login=".$this->login."&passwd=".$this->password; curl_setopt($this->curl, CURLOPT_URL, $url_auth); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $post_login); curl_exec($this->curl); curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookie_file); // записываем файл с куками } /** * @param bool $format * @return array|mixed|null|object */ public function get_histroy($format = false) { curl_setopt($this->curl, CURLOPT_COOKIESESSION, true); curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookie_file); curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3'); curl_setopt($this->curl, CURLOPT_POST, false); curl_setopt($this->curl, CURLOPT_URL, $this->url); $html = curl_exec($this->curl); $result_history = json_decode($html); var_dump($result_history); // если мы авторизованы if(isset($result_history -> success) && $result_history -> success == "1"){ /** * @return json || array */ if($format == false) return $result_history; else return json_decode($html, true); } else{ // если не авторизованы self::auth(); // проходим авторизацию self::get_histroy($format); // запускаем функцию заново } } function __destruct() { curl_close($this->curl); } } 

Call:

 $object = new TestClass("admin", "pass", "+38000000000"); print_r($object ->get_histroy()); 

The problem is that after the authorization method does not give us the data, although the authorization is normal. If you insert var_dump($result_history); as in the example, the data is shown, but it is not output from the class instance. Only if you reload the page, then everything works (already being authorized).

I tried to explain the problem more intelligently.

  • one
    Instead of self::auth(); self::get_histroy($format); self::auth(); self::get_histroy($format); try writing $this->auth(); $this->get_histroy($format); $this->auth(); $this->get_histroy($format); . - mix
  • self:: is a call to static functions that do not use $this and the class is not declared through new , and you have $this in the class and the functions are not static (one depends on the other), so you do not need to use self . - mix
  • @mix unfortunately all the same. - Vladimir
  • @mix thought the cookie file might not have time to create, set sleep (2); but the result is the same. - Vladimir

1 answer 1

Resolved:

 return $this->get_histroy($format); // запускаем функцию заново и возвращаем результат (return) 

It was necessary to return what the function returned.