PHP中使用CURL發(fā)送get/post請(qǐng)求上傳圖片批處理功能
cURL是利用url語(yǔ)法規(guī)定傳輸文件和數(shù)據(jù)的工具。php中有curl拓展,一般用來(lái)實(shí)現(xiàn)網(wǎng)絡(luò)抓取,模擬發(fā)送get post請(qǐng)求,文件上傳。
在php中建立curl的基本步驟如下:
1.初始化
2. 設(shè)置選項(xiàng),包括url
3. 執(zhí)行并獲取結(jié)果
4. 釋放curl句柄。
在工作和學(xué)習(xí)中,我也是時(shí)常用的curl。由于在使用curl設(shè)置選項(xiàng)時(shí),各種選項(xiàng)比較難以記憶,需要參考,故在此記錄下常用的一些例子,以便后來(lái)參考。
實(shí)例一 : 抓取網(wǎng)頁(yè)數(shù)據(jù)(以拉手網(wǎng)開放api為例,也是get請(qǐng)求)
<?php header("Content-type: text/html; charset=utf-8"); $ch = curl_init();//初始化 /*============開始設(shè)置curl各種選項(xiàng)================*/ curl_setopt($ch, CURLOPT_URL, "http://open.lashou.com/opendeals/lashou/city.xml"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $html = curl_exec($ch);//執(zhí)行句柄,獲取返回內(nèi)容 curl_close($ch);//釋放句柄 echo $html
如果用這種方法發(fā)get請(qǐng)求,參數(shù)附加到url后面即可,如curl_setopt($ch, CURLOPT_URL,
"http://localhost/tqj/date/p822.php?name=yyyyy");
實(shí)例二: 利用curl發(fā)送post請(qǐng)求
<?php $uri = "http://localhost/tqj/date/p822.php"; // post參數(shù)數(shù)組 $data = array ( 'name' => 'tianquanjun', 'password' => 'tianquanjun', ); //初始化 $ch = curl_init (); //各種項(xiàng)設(shè)置,網(wǎng)上參考而來(lái),可以查看php手冊(cè),自己設(shè)置 curl_setopt ( $ch, CURLOPT_URL, $uri ); curl_setopt ( $ch, CURLOPT_POST, 1 );//post方式 curl_setopt ( $ch, CURLOPT_HEADER, 0 ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); //執(zhí)行 $return = curl_exec ( $ch ); //釋放 curl_close ( $ch ); print_r($return);
實(shí)例三 :curl 過(guò)程調(diào)試與錯(cuò)誤信息處理
<?php $uri = "http://localhost/tqj/date/p822.php"; // post參數(shù)數(shù)組 $data = array ( 'name' => 'tianquanjun', 'password' => 'tianquanjun', ); //初始化 $ch = curl_init (); //各種項(xiàng)設(shè)置,網(wǎng)上參考而來(lái),可以查看php手冊(cè),自己設(shè)置 curl_setopt ( $ch, CURLOPT_URL, $uri ); curl_setopt ( $ch, CURLOPT_POST, 1 );//post方式 curl_setopt ( $ch, CURLOPT_HEADER, 0 ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); //執(zhí)行 $return = curl_exec ( $ch ); //容錯(cuò)機(jī)制 if($return === false){ var_dump(curl_error($ch)); } //curl_getinfo()獲取各種運(yùn)行中信息,便于調(diào)試 $info = curl_getinfo($ch); echo "執(zhí)行時(shí)間".$info['total_time'].PHP_EOL; //釋放 curl_close ( $ch ); print_r($return); ?>
其中利用curl_error()
獲取錯(cuò)誤信息,curl_getinfo()
獲取運(yùn)行相關(guān)信息。
實(shí)例四: 上傳圖片,獲取返回信息。
跨域上傳圖片,同時(shí)獲取返回信息,這個(gè)就能大顯身手。和post比較像,注意文件之前加一個(gè)@符號(hào)
<?php $uri = "http://localhost/tqj/date/p822.php"; // post參數(shù)數(shù)組 $data = array ( 'author' => 'tianquanjun', 'upload' => '@C:\Users\tianquanjun.DANGDANG\Pictures\a.jpg', ); //初始化 $ch = curl_init (); //各種項(xiàng)設(shè)置,網(wǎng)上參考而來(lái),可以查看php手冊(cè),自己設(shè)置 curl_setopt ( $ch, CURLOPT_URL, $uri ); curl_setopt ( $ch, CURLOPT_POST, 1 );//post方式 curl_setopt ( $ch, CURLOPT_HEADER, 0 ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); //執(zhí)行 $return = curl_exec ( $ch ); //容錯(cuò)機(jī)制 if($return === false){ var_dump(curl_error($ch)); } //curl_getinfo()獲取各種運(yùn)行中信息,便于調(diào)試 $info = curl_getinfo($ch); echo "執(zhí)行時(shí)間".$info['total_time'].PHP_EOL; //釋放 curl_close ( $ch ); print_r($return);
實(shí)例五 : curl批處理。
curl有一個(gè)高級(jí)特性,批處理句柄。允許打開多個(gè)curl鏈接。
批處理就是打開多個(gè)curl句柄,并把這些句柄指派給一個(gè)批處理句柄,然后在while循環(huán)里等待處理完畢。curl_multi_exec()算是稱得上多線程處理,不過(guò)它還是屬于異步的范疇。
<?php header("Content-type: text/html; charset=gbk"); $urls=array('http://www.baidu.com','http://www.qq.com/'); $ch=array(); //批處理句柄 $mh=curl_multi_init(); //打開多個(gè)curl句柄,并指派給一個(gè)批處理句柄 $ch[0]=curl_init($urls[0]); $ch[1]=curl_init($urls[1]); for($i=0;$i<2;$i++) { curl_setopt($ch[$i],CURLOPT_RETURNTRANSFER,1); curl_multi_add_handle($mh,$ch[$i]); } $running = NULL; do{ usleep(10000); curl_multi_exec($mh,$running);//實(shí)現(xiàn)批處理,可以看做curl多線程,實(shí)際是異步范疇 }while($running>0); $res=array(); for($j=0;$j<2;$j++) { $res[$j]=curl_multi_getcontent($ch[$j]); } //關(guān)閉句柄 for($k=0;$k<2;$k++) { curl_multi_remove_handle($mh,$ch[$k]); } curl_multi_close($mh); print_r($res); ?>
基本算是列舉了常用的一些實(shí)例。要想靈活運(yùn)用curl,還是得熟悉curl的各個(gè)設(shè)置項(xiàng),這些設(shè)置項(xiàng)才是curl的靈魂。
總結(jié)
以上所述是小編給大家介紹的PHP中使用CURL發(fā)送get/post請(qǐng)求上傳圖片批處理 功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- PHP中使用cURL實(shí)現(xiàn)Get和Post請(qǐng)求的方法
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- php的curl實(shí)現(xiàn)get和post的代碼
- PHP中的使用curl發(fā)送請(qǐng)求(GET請(qǐng)求和POST請(qǐng)求)
- PHP的curl實(shí)現(xiàn)get,post和cookie(實(shí)例介紹)
- 詳解php用curl調(diào)用接口方法,get和post兩種方式
- php使用CURL模擬GET與POST向微信接口提交及獲取數(shù)據(jù)的方法
- PHP CURL模擬GET及POST函數(shù)代碼
- PHP如何使用cURL實(shí)現(xiàn)Get和Post請(qǐng)求
- php curl發(fā)起get與post網(wǎng)絡(luò)請(qǐng)求案例詳解
- PHP curl get post 請(qǐng)求的封裝函數(shù)示例【get、post、put、delete等請(qǐng)求類型】
相關(guān)文章
Yii調(diào)試查看執(zhí)行SQL語(yǔ)句的方法
這篇文章主要介紹了Yii調(diào)試查看執(zhí)行SQL語(yǔ)句的方法,涉及Yii配置文件的相關(guān)設(shè)置方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07PHP網(wǎng)頁(yè)游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(十二)
這篇文章主要介紹了PHP網(wǎng)頁(yè)游戲Xnova(ogame)源碼解讀研究頁(yè)面部分,需要的朋友可以參考下2014-06-06PHP+Redis鏈表解決高并發(fā)下商品超賣問(wèn)題(實(shí)現(xiàn)原理及步驟)
這篇文章主要介紹了PHP+Redis鏈表解決高并發(fā)下商品超賣問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08PHP設(shè)計(jì)模式之外觀模式(Facade)入門與應(yīng)用詳解
這篇文章主要介紹了PHP設(shè)計(jì)模式之外觀模式(Facade),結(jié)合實(shí)例形式詳細(xì)分析了PHP外觀模式的具體原來(lái)、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-12-12