How to stop PHP repl upon request to certain URL/route?

Question:
So, I’m trying to create a Python Discord Bot that can Start & Stop my PHP Replit with a command. Any way to get that accomplished?

I want to be able to start/stop the Web Server project that I am making, but don’t know how I can accomplish that.

Welcome to Ask! You can’t start and stop Repls on command. You can ping them which generally starts them (assuming they have a webserver, which PHP should), but there is no way to stop them. Perhaps you could combine them both into one Repl and have the bot start and stop a PHP code?

I’m not sure about PHP, but in one of my node.js repls, a request to a certain route will trigger the exec() function to run a shell file that does kill 1 (stops the repl).

1 Like

I have no experience at all with PHP, but here is a AI generated solution. I’ve verified that it works.

Add/change your server routes to look something like below:

<?php
//index.php

$requestMethod = $_SERVER['REQUEST_METHOD'];
$requestUri = $_SERVER['REQUEST_URI'];

if ($requestMethod === 'GET' && $requestUri === '/') {
  echo "Hello, World!";
} elseif ($requestMethod === 'GET' && $requestUri === '/stop-repl') {
  shell_exec('bash stop.sh 2>&1');
} else {
  http_response_code(404);
  echo "Not Found";
}
?>

In the above code, when a GET request sent to /stop-repl, it will execute the stop.sh shell file in the same directory as index.php.

Then create the stop.sh file and add the below code:

#!/bin/bash
kill 1

This should help you stop your repl and a request to the repl again should wake it up too. Of course, you should add extra protection methods because the current code accepts requests from anyone, and so your repl is prone to being stopped by anyone when a request is sent to that URL.

Please do ask if you need further help :grin:

2 Likes