mort Posted January 12, 2010 Share Posted January 12, 2010 Hey all What I am trying to do is write a script that does file_get_contents on a directory of javascript files, and appends each file to the end of a variable. This big string is then used to output to a file and have one big javascript file, redusing HTTP requests on the server. The problem I have is that some of the javascript files I have are actually php files that use header() to read it as javascript, however when I do file_get_contents on a php file, it returns the actual php code instead of the rendered javascript. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/188195-file_get_contents-on-a-php-file/ Share on other sites More sharing options...
salathe Posted January 12, 2010 Share Posted January 12, 2010 Is the PHP only used to send along the header? If so, don't use any PHP at all and let (or make) your web server provide the correct content type header for JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/188195-file_get_contents-on-a-php-file/#findComment-993534 Share on other sites More sharing options...
dgoosens Posted January 12, 2010 Share Posted January 12, 2010 would it be possible to make an exclusion list ? Quote Link to comment https://forums.phpfreaks.com/topic/188195-file_get_contents-on-a-php-file/#findComment-993537 Share on other sites More sharing options...
mort Posted January 12, 2010 Author Share Posted January 12, 2010 not sure what you mean really the php is used to send the header, but it also dynamically builds the javascript as well basically if I have code.php <? echo "hello world"; ?> and do index.php <? echo file_get_contents('code.php'); ?> how do I get that to output hello world instead of <? echo "hello world"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/188195-file_get_contents-on-a-php-file/#findComment-993545 Share on other sites More sharing options...
mort Posted January 12, 2010 Author Share Posted January 12, 2010 also all of my .php javascript files already have header("content-type: application/x-javascript"); Quote Link to comment https://forums.phpfreaks.com/topic/188195-file_get_contents-on-a-php-file/#findComment-993612 Share on other sites More sharing options...
dgoosens Posted January 12, 2010 Share Posted January 12, 2010 maybe you could post one of those files here... also, what about an exclusion list ? Quote Link to comment https://forums.phpfreaks.com/topic/188195-file_get_contents-on-a-php-file/#findComment-993620 Share on other sites More sharing options...
bornecw Posted January 12, 2010 Share Posted January 12, 2010 file_get_contents only retrieves the html of the page after is has been rendered. Try: /** * Fetch the specified page source, even if fopen_url_wrapper is disabled * For that purpose, we use the curl wrapper. It should work everywhere * * @param String $url The url to fetch * @return String The page source code */ function getPage($url="http://www.example.com/"){ $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); curl_setopt($ch,CURLOPT_REFERER,'http://www.google.ch/'); curl_setopt($ch,CURLOPT_TIMEOUT,10); $html=curl_exec($ch); if($html==false){ $m=curl_error(($ch)); error_log($m); } curl_close($ch); return $html; } $html=getPage("http://www.webmaster-talk.com/"); $html=htmlentities($html); echo $html; Quote Link to comment https://forums.phpfreaks.com/topic/188195-file_get_contents-on-a-php-file/#findComment-993622 Share on other sites More sharing options...
mort Posted January 12, 2010 Author Share Posted January 12, 2010 the files themselves are not the problem, they work fine if I use <script type='text/javascript' src='ajax.php'></script> its only when I need to extract the js from that file into a string that it causes issues also i understand what an exclusion list is, can you please explain how it applies to this situation? ta Edit: Ahh curl, good idea! Quote Link to comment https://forums.phpfreaks.com/topic/188195-file_get_contents-on-a-php-file/#findComment-993623 Share on other sites More sharing options...
mort Posted January 12, 2010 Author Share Posted January 12, 2010 cheers bornecw just the trick Quote Link to comment https://forums.phpfreaks.com/topic/188195-file_get_contents-on-a-php-file/#findComment-993626 Share on other sites More sharing options...
salathe Posted January 12, 2010 Share Posted January 12, 2010 You could also just use file_get_contents on the full URL; like with cURL but only one line of code. Quote Link to comment https://forums.phpfreaks.com/topic/188195-file_get_contents-on-a-php-file/#findComment-993632 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.