Ask coding questions

← Back to all posts
Replit DB with PHP?
h
UCYT5040 (14)

How should I access the replit data base with PHP? I'm not sure how the curl commands work or even how to get a env.

Answered by Coder100 (16823) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (16823)

1. .env

ez

<?php
$repl_db_url = $_ENV["REPLIT_DATABASE_URL"];
?>

2. do http requests

still kinda ez

<?php
$repl_db_url = 'look at prev';
$data = array('key1' => 'value1');

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);

$context = stream_context_create($options);
$result = file_get_contents($repl_db_url, false, $context);
if ($result === FALSE) { /* Handle error */ }

var_dump($result);
?>