Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to handle errors in a class written in PHP and using curl. This class uses 3 functions (init, sendFirstForm, sendSecondForm) dependent on one another. The result is tested via nested statements.

I want to manage two types of errors (curl connection errors and form errors) that requires the sending of an email so they can be fixed.

I want to write this code as simple as possible. Is the logic correct?

class Sender {

    public $error;

    public function __construct() {

        $this->error = '';
    }

    public function send() {

        if($this->init() && empty($this->error)) /* account login and retrieval of the cookie */
        {
            if($this->sendFirstForm() && empty($this->error)) /* sending the first form if connection  */
            {
                if($this->sendSecondForm() && empty($this->error)) /* sending the second form if connection  */
                {
                    echo '';
                }
            }
        }
    }

    public function status() {
        return $this->error;
    }

    public function init() {

        // CuRL : account login and retrieval of the cookie
        // ...
        $FORM_URL = 'http://www.domain.com/login.php';

        curl_setopt($ch, CURLOPT_VERBOSE, true);
        $result = curl_exec($ch);

        if(curl_errno($ch))
        {
            $this->error = 'CuRL Error: ' . curl_error($ch) . '<br>';
            return $this->error;
        }
        elseif(curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400) /* Check statut code HTTP */
        {
            $this->error = 'Error in the login form:' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . '<br>';
            return $this->error;
        }       
    }

    public function sendFirstForm() {

        // CuRL : sending the first form
        // ...
        $FORM_URL = 'http://www.domain.com/form1.php';

        curl_setopt($ch, CURLOPT_VERBOSE, true);

        if(curl_errno($ch))
        {
            $this->error = 'CuRL Error: ' . curl_error($ch) . '<br>';
            return $this->error;
        }
        elseif(curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400) /* Check statut code HTTP */
        {
            $this->error = 'Error in the first form:' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . '<br>';
            return $this->error;
        }
        elseif(parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL), PHP_URL_PATH) == parse_url($FORM_URL, PHP_URL_PATH)) /* check if return to form */
        {
            $this->error = 'Error in the first form: $ _POST data missing and / or incorrect' . '<br>';
            return $this->error;

            mail('[email protected]', 'Error Curl', 'Error sending first form, here are the data sent: ' . var_dump($postfields));
        }
    }

    public function sendSecondForm() {

        // CuRL : sending the second form
        // ...
        $FORM_URL = 'http://www.domain.com/form2.php';

        curl_setopt($ch, CURLOPT_VERBOSE, true);

        $result = curl_exec($ch);

        if(curl_errno($ch))
        {
            $this->error = 'CuRL Error: ' . curl_error($ch) . '<br>';
            return $this->error;
        }
        elseif(curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400) /* Check statut code HTTP */
        {
            $this->error = 'Error in the second form:' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . '<br>';
            return $this->error;
        }
        elseif(parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL), PHP_URL_PATH) == parse_url($FORM_URL, PHP_URL_PATH)) /* check if return to form */
        {
            $this->error = 'Error in the second form: $ _POST data missing and / or incorrect' . '<br>';
            return $this->error;

            mail('[email protected]', 'Error Curl', 'Error sending second form, here are the data sent: ' . var_dump($postfields));
        }
    }
}

$send = new Sender();
$send->Send();

if(empty($send->status()))
{
    echo 'Send Successful!';
}
else
{
    echo $send->status();
}
share|improve this question

put on hold as off-topic by 200_success 2 days ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

    
I don't get the feeling that this is real working code. Where, for example, does $postfields come from? – 200_success 2 days ago
    
Really not nice of you @200_success, Why block my question? I just wish advice on how to proceed, I have no particular problem. Thank you to let members share them. cordially – la_chouette 2 days ago
    
We can't review your code if it doesn't actually work. Or, worse, you'll just get uninsightful reviews telling you what's broken. Putting the question on hold gives you a chance to bring it up to standards, because once you get an answer, you won't be allowed to alter the code anymore. – 200_success 2 days ago
    
I did not ask to correct the code, I wonder whether my logic is correct. You seem to be in bad, too bad. I will seek help on another web site. I thank the members who bring me their advice. cordially – la_chouette 2 days ago