Last active
April 7, 2021 15:06
-
-
Save Dan0sz/f298f4efc1888cdca8280a23b0781da7 to your computer and use it in GitHub Desktop.
Script to host any javascript file locally, which can be scheduled using Crontab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Script to update any js-file | |
// Credits go to: Matthew Horne | |
// Remote file to download | |
$remoteFile = 'https://www.google-analytics.com/analytics.js'; | |
$localFile = '/path/to/your/webroot/analytics.js'; | |
// Check if directory exists, otherwise create it. | |
$uploadDir = '/path/to/your/webroot/'; | |
if (!file_exists($uploadDir)) { | |
wp_mkdir_p($uploadDir); | |
} | |
// Connection time out | |
$connTimeout = 10; | |
$url = parse_url($remoteFile); | |
$host = $url['host']; | |
$path = isset($url['path']) ? $url['path'] : '/'; | |
if (isset($url['query'])) { | |
$path .= '?' . $url['query']; | |
} | |
$port = isset($url['port']) ? $url['port'] : '80'; | |
$fp = @fsockopen($host, $port, $errno, $errstr, $connTimeout ); | |
if(!$fp){ | |
// On connection failure return the cached file (if it exist) | |
if(file_exists($localFile)){ | |
readfile($localFile); | |
} | |
} else { | |
// Send the header information | |
$header = "GET $path HTTP/1.0\r\n"; | |
$header .= "Host: $host\r\n"; | |
$header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6\r\n"; | |
$header .= "Accept: */*\r\n"; | |
$header .= "Accept-Language: en-us,en;q=0.5\r\n"; | |
$header .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"; | |
$header .= "Keep-Alive: 300\r\n"; | |
$header .= "Connection: keep-alive\r\n"; | |
$header .= "Referer: http://$host\r\n\r\n"; | |
fputs($fp, $header); | |
$response = ''; | |
// Get the response from the remote server | |
while($line = fread($fp, 4096)){ | |
$response .= $line; | |
} | |
// Close the connection | |
fclose( $fp ); | |
// Remove the headers | |
$pos = strpos($response, "\r\n\r\n"); | |
$response = substr($response, $pos + 4); | |
// Return the processed response | |
echo $response; | |
// Save the response to the local file | |
if(!file_exists($localFile)){ | |
// Try to create the file, if doesn't exist | |
fopen($localFile, 'w'); | |
} | |
if(is_writable($localFile)) { | |
if($fp = fopen($localFile, 'w')){ | |
fwrite($fp, $response); | |
fclose($fp); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there,
I can't get this to work. Using PHP 7.3.8
I had to ' ' line 7 so that it looks like this
The part
I had to remove, did not work
And now I stuck here
It gives me the error
** failed to open stream: No such file or directory in**