2

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

3
  • What's wrong with manually adding the JavaScript? Commented Aug 23, 2013 at 11:52
  • Use DOM to load the HTML and insert your tag. Commented Aug 23, 2013 at 11:53
  • The thing is that by inserting that javascript you wil be able to change the content of the webpage Commented Aug 23, 2013 at 11:56

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

in what way doesnt it work, just saying it doesnt work doesnt help.
@AlejandroMorais, forgot to add $html as the last argument on str_replace, edited answer to reflect, that might be why it didnt work.
Nope. I mean that works, but the thing is that javascript should change the content of the page and it does not change. Other than that, the code works...
then there is a problem with your javascript. have you looked in the javascript console to see if there are errors? That problem should be put into a different question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.