Last active
May 29, 2017 20:46
-
-
Save aurelijusb/50eb18b3f84eb10bfe549acc77525427 to your computer and use it in GitHub Desktop.
Code snippets for VilniusPHP presentation: Headless browsers and friends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Adapted from: https://github.com/ratchetphp/Pawl | |
require __DIR__ . '/vendor/autoload.php'; | |
$loop = React\EventLoop\Factory::create(); | |
$connector = new Ratchet\Client\Connector($loop); | |
// Replace url with one from: curl 127.0.0.1:9222/json | |
$webSocketDebuggerUrl = 'ws://127.0.0.1:9222/devtools/page/384ec8c4-ce82-4105-b133-6741f6dbe8fd'; | |
$connector($webSocketDebuggerUrl, [], ['Origin' => 'http://localhost']) | |
->then(function(Ratchet\Client\WebSocket $conn) { | |
$conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) { | |
echo "Received: {$msg}\n"; | |
if (strpos($msg, '{"method":"Network.loadingFinished"') === 0) { | |
$conn->close(); | |
} | |
}); | |
$conn->on('close', function($code = null, $reason = null) { | |
echo "Connection closed ({$code} - {$reason})\n"; | |
}); | |
$conn->send(json_encode(['id' => 1, 'method' => 'Network.enable'])); | |
$conn->send(json_encode(['id' => 2, 'method' => 'Page.navigate', 'params' => ['url' => 'http://vilniusphp.lt']])); | |
}, function(\Exception $e) use ($loop) { | |
echo "Could not connect: {$e->getMessage()}\n"; | |
$loop->stop(); | |
}); | |
$loop->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = file_get_contents("http://vilniusphp.lt"); | |
$success = preg_match("/.+Susitinkame.+/", $data) | |
&& !preg_match("/.+Warning.+/", $data); | |
$emoji = $success ? ':white_check_mark:' : ':volcano:'; | |
$status = $success ? 'OK' : 'Failed'; | |
// Use value from Slack -> Custom Integrations -> Incoming WebHooks -> Configuration | |
$webHookUrl = 'https://hooks.slack.com/services/AAAAAAAAA/BBBBBBBBB/CCCCCCCCCCCCCCCCCCCCCCCC';; | |
$json = json_encode([ | |
'text' => "$emoji $status", | |
'username' => 'php-status-bot', | |
]); | |
$opts = ['http' => [ | |
'method' => 'POST', | |
'header' => "Content-type: application/json", | |
'content' => $json] | |
]; | |
$context = stream_context_create($opts); | |
$response = file_get_contents($webHookUrl, false, $context); | |
// Example output: Website: OK Sent to slack: ok | |
echo "WebSite: $status Sent to slack: $response"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment