本文主要介紹php curl獲得標(biāo)題(php curl獲得響應(yīng)標(biāo)題),下面一起看看php curl獲得標(biāo)題(php curl獲得響應(yīng)標(biāo)題)相關(guān)資訊。
我們?cè)陂_(kāi)發(fā)php的時(shí)候,經(jīng)常需要對(duì)接口進(jìn)行測(cè)試,或者更方便的是調(diào)用一些已有的模塊的接口來(lái)得到結(jié)果并進(jìn)行后續(xù)操作。我們可以通過(guò)curl模擬提交post和get請(qǐng)求來(lái)實(shí)現(xiàn)這些功能。
以下是curl 發(fā)布和獲取。
?php /** *由phpstorm創(chuàng)建。* us: thinkpad * dat: 2015/7/17 0017 * tim: 13 : 24 */class action {公共靜態(tài)函數(shù)curl _ get($ url){ $ testurl = $ url;$ ch = curl _ init;curl_setopt($ch,curlopt_url,$ testurl);//參數(shù)1表示數(shù)據(jù)傳輸,參數(shù)0表示直接輸出顯示。curl_setopt($ch,curlopt_returntransfer,1);//參數(shù)0表示沒(méi)有頭文件,1表示頭文件curl _ setopt ($ ch,curl _ header,0);$ output = curl _ exec($ ch);curl _ close($ ch);返回$ output}/* * url:訪問(wèn)路徑*要傳遞的array:數(shù)組* */公共靜態(tài)函數(shù)curl _ post ($ url,$ array){ $ curl = curl _ init;//設(shè)置提交的url curl _ setopt ($ curl,curl opt _ url,$ url);//將頭文件信息設(shè)置為數(shù)據(jù)流輸出curl _ setopt ($ curl,curl opt _ header,0);//設(shè)置獲取的信息以文件流的形式返回,而不是直接輸出。curl_setopt($curl,curlopt_returntransfer,1);//設(shè)置post模式提交curl _ setopt ($ curl,curl opt _ post,1);//設(shè)置發(fā)布數(shù)據(jù)$post _ data = $ arraycurl_setopt($curl,curlopt_postfields,$ post _ data);//執(zhí)行命令$ data = curl _ exec($ curl);//關(guān)閉url請(qǐng)求curl _ close($ curl);//獲取數(shù)據(jù)并返回$ data}}之后可以使用curle : : curl _ post($ url,$ array)或者curle : : curl _ get($ url);調(diào)用接口,獲取數(shù)據(jù)。
以上是本文的全部?jī)?nèi)容。希望對(duì)大家有幫助 s學(xué)習(xí),希望大家多多支持。
標(biāo)簽:
設(shè)置數(shù)據(jù)表示文件提交。
了解更多php curl獲得標(biāo)題(php curl獲得響應(yīng)標(biāo)題)相關(guān)內(nèi)容請(qǐng)關(guān)注本站點(diǎn)。