Parse the phone balance from the operator site 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-&gt;login}&amp;PASSWORD={$this-&gt;password}";
$response = $this-&gt;_sendPostRequest($cookieUrl, '', null, 1);
preg_match_all("/PHPSESSID.*;/isU",
            $response, $matches, PREG_PATTERN_ORDER);
$phpsessid = $matches[0][0];
$response = $this-&gt;_sendPostRequest($sessionUrl, $vars, $phpsessid);
preg_match_all("/&lt;SESSION_ID&gt;.*&lt;\/SESSION_ID&gt;/isU",
        $response, $matches, PREG_PATTERN_ORDER);
$sessionId = strip_tags($matches[0][0]);
$vars = "SESSION_ID={$sessionId}";
$response = $this-&gt;_sendPostRequest($balanceUrl, $vars);
preg_match_all("/&lt;div class=\"balance_good td_def\"&gt;.*&lt;\/div&gt;/isU",
        $response, $matches, PREG_PATTERN_ORDER);
if (count($matches[0]) == 0) {
    preg_match_all("/&lt;div class=\"balance_not_good td_def\"&gt;.*&lt;\/div&gt;/isU",
            $response, $matches, PREG_PATTERN_ORDER);
}
$this-&gt;balance = strip_tags($matches[0][0]);
$this-&gt;updated = date(Settings_Constants::DATETIME_DATABASE_FORMAT);
$this-&gt;save();
$this-&gt;balance;
It is not perfect example, but your code might be like this:

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

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

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

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

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