jquery+thinkphp實現跨域抓取資料的方法

來源:互聯網
上載者:User

標籤:function   不清楚   inf   size   字串   擴充   函數   substr   php   

jquery的$.post發送資料到伺服器後台,在由背景PHP代碼執行遠程抓取,存到資料庫ajax返回資料到前台,前台用JS接受資料並顯示。

//遠程抓取擷取資料
$("#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";
});

上面的jquery代碼還算四樓比較清楚的,糾結我的就是那個json資料的接收

json = eval("(" + $data + ")");
//eval() 函數可計算某個字串,並執行其中的的 javascript 代碼。

其實這個都還是前台,跨域抓取是用php的擴充simple_html_dom完成的(不清楚的可以到網上去搜一搜,基於PHP5開發的)

把遠端頁面抓取到本地。

import("@.ORG.simple_html_dom");
//thinkphp內匯入擴充,你要把網上下載的代碼改名為simple_html_dom.class.php放到APPNAME\Lib\ORG的目錄下面
$html = file_get_html(‘http://openoj.awaysoft.com/JudgeOnline/userinfo.php?user=‘.$username); //遠程抓取了
$ret = $html->find(‘center‘,0)->plaintext; //返回資料了。

如下所示:

<strong><span style="font-size:18px;">/*編號=年份後兩位+月份+id四位元*/ 

$id = $this->student_model->save(0, $data); 
$sn = date(‘Y‘, time()); 
$sn = substr($sn, -2); 
$sn.= date(‘m‘, time()); 
$sn.=sprintf("%04d", $id);</span></strong> 

jquery+thinkphp實現跨域抓取資料的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.