How to use file_get_contents on Ajax site

感情迁移 提交于 2020-01-13 11:36:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!