Horje

Tips (Total 5)


# Tips-1) How to add PHP File Get Content

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.

Full Code

index.php
Example: PHP
<!DOCTYPE html>   
<body> 
    <?php 
        // Reading contents from the 
        // geeksforgeeks homepage 
        $homepage = file_get_contents( 
            "https://horje.com/"); 
        echo $homepage; 
    ?> 
</body> 
</html>

Output should be:

Full Code

# Tips-2) How to add PHP cURL CURLOPT

PHP cURL is a PHP Function which request an URL to view the page sources.

Full Code of PHP cURL CURLOPT

index.php
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;
?>

# Tips-3) How to load XML Sitemap URLs

A way to load xml sitemap its url

How to extract URLs from xml sitemap on PHP

An Easy way to extract urls from xml sitemap
index.php
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 } ?>

Output should be:

How to extract URLs from xml sitemap on PHP

# Tips-4) How to get file size from a remote url in PHP

Here I make a simple code.

Full Example of Extract File Size from url

Follow the Example.
index.php
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;
?>

Output should be:


# Tips-5) How to create a zip file from url using PHP

What are that?
  1. Create zip
  2. It will be from url
  3. Download zip

Try following code

See the Example.
index.php
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);
?>