Recover errors with php and send via message

Asked

Viewed 59 times

1

I am doing an integration of Slack with my application to show every time any system error will arrive a message in Slack channel.

using the API I can already send message through Curl: follow the code>

function slack($message, $room = "unique", $icon = ":ghost:") {

$data = array(
    "username" => "slackbot",
    "channel" => "#canal",
    "text" => $message,
    "mrkdwn" => true,
    "icon_url" => $icon,
    "attachments" => array(
         array(
            "color" => "#b0c4de",
        //  "title" => $message_primary_title,
            "fallback" => "fallback",
            "text" => "attachments",
            "mrkdwn_in" => array(
                "fallback",
                "text"
            ),
            "fields" => array(
                array(
                    "title" => "fileds tittle",
                    "value" => "fields value"
                )
            )
        )
    )
);
$json_string = json_encode($data);
$slack_webhook_url = "minha url da api";

$slack_call = curl_init($slack_webhook_url);
curl_setopt($slack_call, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($slack_call, CURLOPT_POSTFIELDS, $json_string);
curl_setopt($slack_call, CURLOPT_CRLF, true);
curl_setopt($slack_call, CURLOPT_RETURNTRANSFER, true);
curl_setopt($slack_call, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "Content-Length: " . strlen($json_string))
);

$result = curl_exec($slack_call);
curl_close($slack_call);

 return $result;   
}

Now missing to format the message to Slack with the errors. Link where error occurs, line, error type.

inserir a descrição da imagem aqui

For example, I wanted to take these variables that php shows when an error occurs.

wanted it to work more or less that way I put down >

$hook['pre_controller'] = function() {
//se achar erros chama o método que envia mensagem pro slack enviando os dados

if($error){
        $message = "erro no sistema ;". $error['message'];
        $slack = slack($message, $linha, $tipo);
    }
}

If anyone can help, I’m very grateful.

  • I removed the tags "javascript", "Laravel", "codeigniter" and "webhooks" as they do not seem to have any relation to the question. If I did wrong please correct me.

  • is why I’m using codeigniter and the framework’s Hooks.php. I think javascript and Laravel really got it wrong. vlw

1 answer

0

Gazing here we can easily see the methods that PHP’s Exception class has. All this information is recoverable via these methods, so just format your message the way you want! =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.