Last active
July 3, 2017 18:58
-
-
Save aurelijusb/ebbd1bda53f44645df73644a92a3991a to your computer and use it in GitHub Desktop.
Code snippets for Minsk PHP Night 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); | |
// For example: ws://127.0.0.1:9222/devtools/page/6c98af2a-8944-4742-91d3-154cbd873ce2 | |
$webSocketUrl = json_decode(`curl 127.0.0.1:9222/json`, true)[0]['webSocketDebuggerUrl']; | |
$connector($webSocketUrl, [], ['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://eventspace.by/']])); | |
}, 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://eventspace.by"); | |
// Expected and unexpected phrases | |
$success = preg_match("/.+объединяющая.+/", $data) | |
&& !preg_match("/.+Warning.+/", $data); | |
// Formatting output for Slack | |
$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', | |
]); | |
// PHP streams magic to simulate POST request | |
$opts = ['http' => [ | |
'method' => 'POST', | |
'header' => "Content-type: application/json", | |
'content' => $json] | |
]; | |
$context = stream_context_create($opts); | |
// Sending to Slack WebHooks | |
$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