The file_get_contents() function in PHP is an inbuilt function that is used to read a file into a string. The function uses memory mapping techniques that are supported by the server and thus enhance the performance making it a preferred way of reading the contents of a file. The path of the file to be read, which is sent as a parameter to the function and it returns the data read on success and FALSE on failure.
Example:
PHP
<!DOCTYPE html>
<body>
<?php
// Reading contents from the
// geeksforgeeks homepage
$homepage = file_get_contents(
"https://horje.com/");
echo $homepage;
?>
</body>
</html>
PHP cURL is a PHP Function which request an URL to view the page sources.
Example:
PHP
<?php
$urlssss1='https://horje.com';
// Step 1
$cSession = curl_init();
// Step 2
curl_setopt($cSession,CURLOPT_URL,''.$urlssss1.'');
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
// Step 3
$d221=curl_exec($cSession);
echo $d221;
?>
A way to load xml sitemap its url
Example:
PHP
<?php
$affectedRow = 0;
$url1 = 'https://wordpress.org/plugins/sitemap-2.xml';
$xml = simplexml_load_file("$url1") or die('Sorry');
foreach ($xml->children() as $row) {
$link = $row->loc;
echo $link;
echo '</br>';
if (! empty($result)) {
$affectedRow ++;
} else {
$error_message = mysqli_error($conn) . "\n";
}
}
if ($affectedRow > 0) {
$message = $affectedRow . " URLs have been crawled. It will index in the next crawl date";
} else {
$message = "No records inserted";
}
?>
<div class="affected-row"><?php echo $message; ?></div>
<?php if (! empty($error_message)) { ?>
<div class="error-message"><?php echo nl2br($error_message); ?></div>
<?php } ?>
Here I make a simple code.
Example:
PHP
<?php
function fsize($path) {
$fp = fopen($path,"r");
$inf = stream_get_meta_data($fp);
fclose($fp);
foreach($inf["wrapper_data"] as $v) {
if (stristr($v, "content-length")) {
$v = explode(":", $v);
return trim($v[1]);
}
}
return 0;
}
?>
<?php
$file = "https://downloads.wordpress.org/plugin/wordpress-seo.22.6.zip";
$inbytes = fsize($filesize);
echo $inbytes;
?>
Example:
PHP
<?php
# define file array
$files = array(
'https://www.google.com/images/logo.png',
'https://en.wikipedia.org/static/images/project-logos/enwiki-2x.png',
);
# create new zip object
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach ($files as $file) {
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file), $download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
header('Content-disposition: attachment; filename="my file.zip"');
header('Content-type: application/zip');
readfile($tmp_file);
unlink($tmp_file);
?>