Jquery + thinkphp implements cross-Origin data capturing, jquerythinkphp
This example describes how to capture data across domains using jquery + thinkphp. We will share this with you for your reference. The details are as follows:
Today, we provide a remote data capture function. Remember that jquery can use Ajax to remotely capture data, but it cannot cross-origin. I found a lot on the Internet. However, I think it is still comprehensive, so I think it is a bit complicated, but at least it has been solved:
Effect of capturing data to the local database through cross-origin and then asynchronously updating the data
Implementation Method: jquery's $. post sends data to the backend of the server. The PHP code in the backend is remotely captured and stored in the database. ajax returns data to the front end. The front end receives and displays the data using JS.
// Remotely capture and obtain data $ ("# update_ac "). click (function () {$ username = $ ("# username "). text (); $ ("# AC, # rank, # Submit, # solved, # solved2, # solved3 "). ajaxStart (function () {functions (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 above jquery code is clear on the fourth floor, and I am entangled in receiving the json data.
Json = eval ("(" + $ data + ")"); // The eval () function can calculate a string and execute the JavaScript code.
In fact, this is still the front-end. Cross-origin crawling is completed using the php extension simple_html_dom (you can go to the Internet to search for it, developed based on PHP5)
Capture the remote page to the local device.
Import ("@. ORG. simple_html_dom "); // import extension in thinkphp. You need to rename the code downloaded from the Internet to simple_html_dom.class.php and put it under the directory of APPNAME \ Lib \ ORG $ html = file_get_html ('HTTP: // openoj.awaysoft.com/JudgeOnline/userinfo.php? User = '. $ username); // $ ret = $ html-> find ('center', 0)-> plaintext; // The returned data is remotely captured.
The above code is only the core code. simple_html_dom also has many functions. Learn it by yourself.
The returned data is a string, and then the regular expression is used to filter the required data.