问题
When you use file_gets_contents($website)
or cURL to load a website, does it load the whole website? I am mostly interested about using cURL.
I am using it to load a webpage that then gets some contents such as price using AJAX and it has some problems getting the prices.
When I use file_get_contents, does it load as normally as a whole website does on a browser plus the stuff loaded using Ajax?
回答1:
No. Using file_get_contents()
will only return the page contents, it will not execute any JavaScript on the page itself. The analog of this behaviour is almost equivalent to "View Page Source" in a browser.
回答2:
The snippet
$website = 'http://stackoverflow.com/';
file_gets_contents($website)
loads the result of the HTTP-request, nothing else. Thus, the call loads the source of the html-page returned by the URL http://stackoverflow.com/
.
Especially, file_gets_contents()
does not load stuff referenced by the page pointed to by http://stackoverflow.com/
.
Evaluating JavaScript code using PHP
In case you'd like to evaluate JavaScript inside of HTML-code using a PHP-script, you'd probably wish to use the V8 JavaScript engine, which needs to be compiled into your PHP-binary:
- http://php.net/manual/en/book.v8js.php
Find an example how to use the V8 JavaScript engine here.
来源:https://stackoverflow.com/questions/13978016/how-to-use-file-get-contents-on-ajax-site