Анализ баланса телефона с сайта оператора


Как я могу написать этот код лучше?

$cookieUrl = 'https://-------------/ps/scc/login.php';
$sessionUrl = 'https://-------------/ps/scc/php/check.php';
$balanceUrl = 'https://-------------/SCWWW/ACCOUNT_INFO';
$vars = "LOGIN={$this->login}&PASSWORD={$this->password}";
$response = $this->_sendPostRequest($cookieUrl, '', null, 1);
preg_match_all("/PHPSESSID.*;/isU",
            $response, $matches, PREG_PATTERN_ORDER);
$phpsessid = $matches[0][0];
$response = $this->_sendPostRequest($sessionUrl, $vars, $phpsessid);
preg_match_all("/<SESSION_ID>.*<\/SESSION_ID>/isU",
        $response, $matches, PREG_PATTERN_ORDER);
$sessionId = strip_tags($matches[0][0]);
$vars = "SESSION_ID={$sessionId}";
$response = $this->_sendPostRequest($balanceUrl, $vars);
preg_match_all("/<div class=\"balance_good td_def\">.*<\/div>/isU",
        $response, $matches, PREG_PATTERN_ORDER);
if (count($matches[0]) == 0) {
    preg_match_all("/<div class=\"balance_not_good td_def\">.*<\/div>/isU",
            $response, $matches, PREG_PATTERN_ORDER);
}
$this->balance = strip_tags($matches[0][0]);
$this->updated = date(Settings_Constants::DATETIME_DATABASE_FORMAT);
$this->save();
$this->balance;


395
4
задан 13 апреля 2011 в 06:04 Источник Поделиться
Комментарии
1 ответ

Это не идеальный пример, но ваш код может быть такой:

function __construct()
{
//...
$this->cookieUrl = 'https://-------------/ps/scc/login.php';
$this->sessionUrl = 'https://-------------/ps/scc/php/check.php';
$this->balanceUrl = 'https://-------------/SCWWW/ACCOUNT_INFO';
//...
}

function loginToService()
{
$response = $this->_sendPostRequest($this->cookieUrl, '', null, 1);
preg_match_all("/PHPSESSID.*;/isU",
$response, $matches, PREG_PATTERN_ORDER);
$this->phpsessid = $matches[0][0];
if (!this->phpsessid) {
throw new Exception ('Could not retrieve PHP session id');
}
}

function getServiceSessionId()
{
$vars = array(
'LOGIN' => $this->login,
'PASSWORD' => $this->password,
);
$response = $this->_sendPostRequest($this->sessionUrl, implode('&', $vars), $this->phpsessid);
preg_match_all("/<SESSION_ID>.*<\/SESSION_ID>/isU",
$response, $matches, PREG_PATTERN_ORDER);
$this->sessionId = strip_tags($matches[0][0]);
if (!$this->sessionId) {
throw new Exception ('Could not retrieve Service session id');
}
}

function getBalanceMessage()
{
$vars = "SESSION_ID={$sessionId}";
$response = $this->_sendPostRequest($balanceUrl, $vars);
preg_match_all("/<div class=\"balance_good td_def\">.*<\/div>/isU",
$response, $matches, PREG_PATTERN_ORDER);
if (count($matches[0]) == 0) {
preg_match_all("/<div class=\"balance_not_good td_def\">.*<\/div>/isU",
$response, $matches, PREG_PATTERN_ORDER);
}
retrun strip_tags($matches[0][0]);
}

/* begin */
try {
$this->loginToService();
$this->getServiceSessionId();
} catch Exception $e {
return $e->getMessage();
}
$this->balance = $this->getBalanceMessage();;
$this->updated = date(Settings_Constants::DATETIME_DATABASE_FORMAT);
$this->save();
$this->balance; //What the hell is this?
/* end */

3
ответ дан 13 апреля 2011 в 10:04 Источник Поделиться