The example of this article describes the method of jquery+thinkphp fetching data across domains. Share to everyone for your reference, specific as follows:
Today do a remote crawl data function, remember jquery can use AJAX remote crawl, but not cross-domain. Find a lot on the Internet. But I think it's a combination, so I now feel a little complicated with simple questions, but at least for now:
Effect of cross-domain fetching data to local database and then asynchronous update
I realize the way: jquery $.post send data to the server background, in the background of the PHP code to perform remote crawl, save to the database Ajax return data to the foreground, the foreground with JS data and display.
Remote crawl Fetch 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 on the top of the four-storey is clear, the tangle of my is that JSON data receive
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 completed with the expansion of PHP simple_html_dom (not clear to the Internet to search, based on PHP5 development)
Crawl the remote page to the local.
Import ("@. Org.simple_html_dom ");
thinkphp import extension, you have to change the code to download the Internet to simple_html_dom.class.php placed under the appname\lib\org directory
$html = file_get_html (' http:// Openoj.awaysoft.com/judgeonline/userinfo.php?user= '. $username); Remotely crawled
$ret = $html->find (' center ', 0)->plaintext;//returned data.
The code above is just the core code, and Simple_html_dom has many features to extend. Get to know it yourself.
The returned data is a string, and the regular expression is used to filter the desired data. Here's the effect chart.
More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."
I hope this article will help you with the PHP program design based on thinkphp framework.