I need to POST to a url with php code in order to make a phone call. This is an example of javascript:
<script>
function callme() {
var XHR = new XMLHttpRequest();
XHR.addEventListener('readystatechange', function() {
if (XHR.readyState == 4 && XHR.status == 200) {
document.getElementById("result").innerHTML = XHR.responseText;
} else {
document.getElementById("result").innerHTML = "ha ocurrido un error: " + XHR.responseText
}
});
var FD = new FormData();
FD.append('number', document.getElementById("number").value);
XHR.open('POST', 'doC2c.php');
document.getElementById("result").innerHTML = "calling ..."
XHR.send(FD);
}
</script>
And the doC2c.php file must be:
<?php
$apikey = ""; //API KEY MUST BE PUT HERE GOT FROM DB
$origin = ""; //ORIGIN EXT MUST BE PUT HERE OR GET FROM SOMEWHERE
if ($apikey == "") {
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);
echo "APIKEY MISSING";
die();
} else if ($origin == "") {
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);
echo "ORIGIN MISSING";
die();
} else {
$opts = array(
'http' => array(
'method' => "GET",
'header' => "Content-type: application/json\n"
."X-Api-Key: ".$apikey,
)
);
$context = stream_context_create($opts);
$response = file_get_contents("https://....../api/originatecall/".$origin."/".$_POST['number'], false, $context);
//echo $response;
echo "CALLING...";
}
?>
I would like to make a D8 module that create a URL I could POST to execute the php code, but I want to have Drupal API to query de DB and so on. I know how to make a module with a form and a route to show it, but I don't know how assign a route to a php code file.