Jump to content

file_get_contents on a php file


mort

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/188195-file_get_contents-on-a-php-file/
Share on other sites

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";  ?>

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;  

 

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! :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.