This article mainly introduces jquery + thinkphp to implement cross-origin data capturing methods, and analyzes thinkPHP and jQuery ajax to implement cross-origin data capturing operations in combination with examples, for more information about how to use jquery + thinkphp to capture data across domains, see the following example. 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 ("") ;}); $. 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.
I hope this article will help you design PHP programs based on the ThinkPHP framework.
For more information about how to use jquery + thinkphp to capture data across domains, see The PHP Chinese website!