HiveBrain v1.2.0
Get Started
← Back to all entries
snippetphpMinor

Parse the phone balance from the operator site

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theoperatorbalanceparsesitephonefrom

Problem

How can I write this code better?

$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("/.*/isU",
        $response, $matches, PREG_PATTERN_ORDER);
$sessionId = strip_tags($matches[0][0]);
$vars = "SESSION_ID={$sessionId}";
$response = $this->_sendPostRequest($balanceUrl, $vars);
preg_match_all("/.*/isU",
        $response, $matches, PREG_PATTERN_ORDER);
if (count($matches[0]) == 0) {
    preg_match_all("/.*/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;

Solution

It is not perfect example, but your code might be like this:

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("/.*/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("/.*/isU",
            $response, $matches, PREG_PATTERN_ORDER);
    if (count($matches[0]) == 0) {
        preg_match_all("/.*/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 */

Code Snippets

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 */

Context

StackExchange Code Review Q#1844, answer score: 3

Revisions (0)

No revisions yet.