Fallback Javascript with PHP from CDN to Local Storage on Unavailable -
i need help. create fallback sourcecode. using cdn host javascript , css , images. trying create fallback in php instance ip 123.456.789 not available in 400ms php falback fetch local storage @ 987.654.321
so thinking on lines of (bare me, amatuer example)
if ip 123.456.789 in 400ms //show file 123.456.789 else //show file 987.654.321
i have source code hard coded such as:
<script type="text/javascript" src="123.456.789/js/jquery.js"></script>
so if 123.456.789 not available in 400ms 1 load
<script type="text/javascript" src="987.654.321/js/jquery.js"></script>
i answered own question while ago. decided share here.
<?php //debug off default error_reporting(0); //cdn fallback if (!isset($_get['aws_cdn'])) { $cdn_fallback = curl_init('http://123.456.789/'); //set cdn ip or url curl_setopt($cdn_fallback, curlopt_returntransfer, true); curl_setopt($cdn_fallback, curlopt_nosignal, 1); curl_setopt($cdn_fallback, curlopt_timeout_ms, 400); //how many milliseconds before fallback local storage $data = curl_exec($cdn_fallback); $curl_errno = curl_errno($cdn_fallback); $curl_error = curl_error($cdn_fallback); curl_close($cdn_fallback); if ($curl_errno > 0) { ?> <script type="text/javascript" src="http://example.com/js/jquery.js"></script> <?php } else { ?> <script type="text/javascript" src="http://123.456.789/cdn/js/jquery.js"></script> <?php }} ?>
Comments
Post a Comment