Created
April 23, 2023 20:34
-
-
Save JohnRDOrazio/157193a9cd723473f6a7d634cee464bb to your computer and use it in GitHub Desktop.
a simple PHP helper script used on the Semina Verbi wiki project, to retrieve Bible quotes from the BibleGet API and store them locally
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 | |
header('Content-Type: application/json'); | |
$DATA = new stdClass(); | |
$DATA->http_referer = $_SERVER['HTTP_REFERER']; | |
/** | |
* Only allow access to this script via ajax requests from this same server | |
* if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest' ){ | |
* $DATA->message = "Not an ajax request"; | |
* return json_encode($DATA); | |
* } else { | |
*/ | |
$BGET = []; | |
if ($_SERVER['REQUEST_METHOD'] === 'POST'){ | |
if(isset($_POST["version"])){ | |
$BGET["version"] = $_POST["version"]; | |
} else { | |
$DATA->message = "Version not set"; | |
echo json_encode($DATA); | |
die(); | |
} | |
if(isset($_POST["ref"])){ | |
$BGET["ref"] = $_POST["ref"]; | |
} else { | |
$DATA->message = "Quote reference not set"; | |
echo json_encode($DATA); | |
die(); | |
} | |
} else if($_SERVER['REQUEST_METHOD'] === 'GET'){ | |
if(isset($_GET["version"])){ | |
$BGET["version"] = $_GET["version"]; | |
} else { | |
$DATA->message = "Version not set"; | |
echo json_encode($DATA); | |
die(); | |
} | |
if(isset($_GET["ref"])){ | |
$BGET["ref"] = $_GET["ref"]; | |
} else { | |
$DATA->message = "Quote reference not set"; | |
echo json_encode($DATA); | |
die(); | |
} | |
} | |
$str = $BGET["version"] . "/" . $BGET["ref"]; | |
$tmp = preg_replace("/\s+/", "", $str); | |
$hash = md5($tmp); | |
if(file_exists("bibleQuotes/{$hash}.html") ){ | |
$DATA->message = "File $hash.html exists, we do not need to make a request to the BibleGet server"; | |
$DATA->html = file_get_contents("bibleQuotes/{$hash}.html"); | |
} else { | |
$DATA->message = "File $hash.html not found, we will now make a request to the BibleGet server"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, "https://query.bibleget.io"); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch,CURLOPT_POSTFIELDS, "query={$BGET["ref"]}&version={$BGET["version"]}&appid=SeminaVerbi&return=html"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
$output = str_replace(array("\n", "\r"), '', $output); | |
$output = preg_replace('/<br( ){0,1}(\/){0,1}>/','',$output); | |
$output = preg_replace('/<(sm|pof|po|pol|pos|poif|poi|poil|po3|po3l|speaker|pr)>/', '<span class="$1">', $output); | |
$output = preg_replace('/<\/(sm|pof|po|pol|pos|poif|poi|poil|po3|po3l|speaker|pr)>/', '</span>', $output); | |
$output = preg_replace('/<(\/){0,1}i>/', '<$1i>', $output); | |
file_put_contents("bibleQuotes/{$hash}.html",$output); | |
$DATA->html = $output; | |
} | |
echo json_encode($DATA,JSON_UNESCAPED_UNICODE); | |
die(); | |
/* | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment