Starting point for reading, negative value means starting from end of file.
length:
Maximum length of data read
URL ( localhost) to get content
We will keep our function in one main file and try to read another file by sending request.
In second file we will keep minimum code to just return a welcome message with name. From our main file we will send the name by using GET method.
Here is a sample code to get output content from a file. While sending the request we will send name variable and try to get the welcome message with the name. We have used localhost for this. You should use your host or path in this code.
Output: here we are not getting the source code , only the output as returned by the browser is read as string and displayed.
Welcome Alex John
You can change the output by changing the start and max_length optional parameters of the function.
Getting external URL content
This is a security risk to allow file_get_contents(). It can be used to get content from external URL if allow_url_fopen is set to On in php.ini file.
my_file.txt
Welcome to plus2net
Read PHP section here
Getting mututal fund NAV data
This optimized PHP script leverages the MFAPI service to fetch real-time Mutual Fund NAV data with high precision. Unlike traditional methods that require parsing massive text files from AMFI, this approach uses a targeted API call to retrieve a specific scheme's details in JSON format. This drastically reduces bandwidth usage and server-side processing time, making it the perfect solution for performance-critical applications like live investment trackers or financial blog widgets.
<?php// Set the specific AMFI scheme code$schemeCode = "120847";
$apiUrl = "https://api.mfapi.in/mf/" . $schemeCode . "/latest";
// Bypass SSL verification for local environments$context = stream_context_create([
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false,
],
]);
// Fetch the JSON response$jsonResponse = file_get_contents($apiUrl, false, $context);
$data = json_decode($jsonResponse, true);
if (isset($data['data'][0]['nav'])) {
$fundName = $data['meta']['scheme_name'];
$navValue = $data['data'][0]['nav'];
$navDate = $data['data'][0]['date'];
echo"Fund: " . $fundName . " | NAV: " . $navValue . " (as of " . $navDate . ")";
} else {
echo"Error: Scheme data not found.";
}
?>