I am using Php with react js ,there is constant declared in react js constant file that is used all over the application ,initially it was hardcoded in react js like Export const thumburl="www.example.com"; and its imported in almost every part of application now the requirement is to generate this URL dynamically from db.so i created a php file which will return this url i want to use it in react js constant file and export that ,tried several things but wasn t able to get results. it would be great help if any one can provide me the full working method to do this Note : react js is not my domain
async function githubUsers() {
let response = await fetch( https://example.com/api.php )
let users = await response.json()
return users;
}
export const thumbUrl=githubUsers();
tried above code but it couldn t assign the value below is the sample php code
<?php
if (isset($_SERVER[ HTTP_ORIGIN ])) {
header("Access-Control-Allow-Origin: {$_SERVER[ HTTP_ORIGIN ]}");
header( Access-Control-Allow-Credentials: true );
header( Access-Control-Max-Age: 86400 ); // cache for 1 day
}
if ($_SERVER[ REQUEST_METHOD ] == OPTIONS ) {
if (isset($_SERVER[ HTTP_ACCESS_CONTROL_REQUEST_METHOD ]))
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
if (isset($_SERVER[ HTTP_ACCESS_CONTROL_REQUEST_HEADERS ]))
header("Access-Control-Allow-Headers: {$_SERVER[ HTTP_ACCESS_CONTROL_REQUEST_HEADERS ]}");
}
$data = www.example.com/gallery/thumbs/ ;
header( Content-Type: application/json );
echo json_encode($data);