Question

Is there a way to insert a javascript at the end of the file_get_contents() ?

I don't mean adding to the output of the function, the javascript. What I want is to put a script at the end of the website I'm getting from the file_get_contents().

Is like using a extension of chrome, which inserts a javascript at the end of the website. But in my case I want the function file_get_contents($url) to output directly everything (the website + javascript inserted).

Is there a way to use cUrl in this situation ?

Is there a way to accomplish this ?

Thanks

Was it helpful?

Solution

you would need to use *_replace functions on the retrieved code, or use DOM classes to alter the code. There is no way to modify file_get_contents itself to add code

$script =<<<END
   <script type="text/javascript" src="http://www.example.com/mysscript.js"></script>
   </body>
END;
$html = file_get_contents($url);
$html = str_replace("</body>",$script,$html);

This assumes the content being grabbed has full html code, mainly includes the </body> tag

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top