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();
}
$postfields
come from? – 200_success♦ 2 days ago