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

Link to comment
Share on other sites

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;  

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.