In this paper, we describe a method for jquery+thinkphp to fetch data across domains. Share to everyone for your reference, as follows:
Today do a remote fetch data function, remember that jquery can be used for remote crawling Ajax, but not cross-domain. Find a lot of online. But I think it's a comprehensive one, so I now feel a bit complicated by the simple problem, but at least for now it's solved:
The effect of crawling data across domains to the local database and then updating asynchronously
I realized the way: jquery $.post send data to the server background, in the background of the PHP code to perform a remote crawl, stored in the database Ajax return data to the foreground, the front desk with JS to accept data and display.
Remote fetch gets data $ ("#update_ac"). Click (function () { $username = $ ("#username"). Text (); $ ("#AC, #rank, #Submit, #solved, #solved2, #solved3"). Ajaxstart (function () { $ (this). HTML ("<span class=" Loading2 ' > </span> "); }); $.post ("update_ac/username/" + $username, {},function ($data) { json = eval ("(" + $data + ")"); $ ("#Submit"). HTML (json.data.Submit); $ ("#AC"). HTML (Json.data.AC); $ ("#solved, #solved2, #solved3"). HTML (json.data.solved); $ ("#rank"). HTML (Json.data.rank); } ), "JSON";});
The jquery code above is still on the four floor of the more clear, tangled my is that the JSON data received
JSON = eval ("(" + $data + ")"); the//eval () function computes a string and executes the JavaScript code in it.
In fact, this is still the foreground, cross-domain crawl is the extension of PHP Simple_html_dom completed (not clear can go to the Internet search, based on the development of PHP5)
Fetch the remote page to the local.
Import ("@.org.simple_html_dom"); in//thinkphp, you have to rename the code downloaded online to simple_html_dom.class.php to the appname\lib\org directory $html = file_ get_html (' http://openoj.awaysoft.com/JudgeOnline/userinfo.php?user= '. $username); Remote Fetch $ret = $html->find (' center ', 0)->plaintext; Return the data.
The code above is just the core code, and there are many more features of the simple_html_dom extension. Get to know it yourself.
The returned data is a string, and the regular expression is used to filter the required data.
It is hoped that this article is helpful to the PHP program design based on thinkphp framework.