Jquery+thinkphp method for realizing cross-domain fetching data _php instance

Source: Internet
Author: User
Tags eval php code php template smarty template

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.