欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂PHP培訓(xùn)簡述PHP中使用CURL發(fā)送get/post請求上傳圖片批處理功能

2019-05-15 13:22:01 4051瀏覽

本篇文章扣丁學(xué)堂PHP培訓(xùn)小編給讀者們分享一下PHP中使用CURL發(fā)送get/post請求上傳圖片批處理功能,對PHP開發(fā)技術(shù)感興趣的小伙伴就隨小編來了解一下,希望對小伙伴們有幫助。


扣丁學(xué)堂PHP培訓(xùn)簡述PHP中使用CURL發(fā)送get/post請求上傳圖片批處理功能


CURL是利用url語法規(guī)定傳輸文件和數(shù)據(jù)的工具。PHP中有curl拓展,一般用來實現(xiàn)網(wǎng)絡(luò)抓取,模擬發(fā)送get post請求,文件上傳。


在PHP中建立curl的基本步驟如下:


1、初始化

2、設(shè)置選項,包括url

3、執(zhí)行并獲取結(jié)果

4、釋放curl句柄。

    

在工作和學(xué)習(xí)中,我也是時常用的curl。由于在使用curl設(shè)置選項時,各種選項比較難以記憶,需要參考,故在此記錄下常用的一些例子,以便后來參考。



實例一:抓取網(wǎng)頁數(shù)據(jù)(以拉手網(wǎng)開放api為例,也是get請求)


<?php
header("Content-type: text/html; charset=utf-8"); 
$ch = curl_init();//初始化
/*============開始設(shè)置curl各種選項================*/
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請求,參數(shù)附加到url后面即可,如curl_setopt($ch, CURLOPT_URL。


    

例二: 利用curl發(fā)送post請求


<?php
$uri = "http://localhost/tqj/date/p822.php";
// post參數(shù)數(shù)組
$data = array (
  'name' => 'tianquanjun',
  'password' => 'tianquanjun',
);
//初始化
$ch = curl_init ();
//各種項設(shè)置,網(wǎng)上參考而來,可以查看php手冊,自己設(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);


實例三 :curl 過程調(diào)試與錯誤信息處理


<?php
$uri = "http://localhost/tqj/date/p822.php";
// post參數(shù)數(shù)組
$data = array (
  'name' => 'tianquanjun',
  'password' => 'tianquanjun',
);
//初始化
$ch = curl_init ();
//各種項設(shè)置,網(wǎng)上參考而來,可以查看php手冊,自己設(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 );
//容錯機制
if($return === false){
 var_dump(curl_error($ch));
 }
//curl_getinfo()獲取各種運行中信息,便于調(diào)試 
$info = curl_getinfo($ch);
echo "執(zhí)行時間".$info['total_time'].PHP_EOL;
//釋放
curl_close ( $ch );
print_r($return);
?>

其中利用curl_error()獲取錯誤信息,curl_getinfo()獲取運行相關(guān)信息。



實例四: 上傳圖片,獲取返回信息。


跨域上傳圖片,同時獲取返回信息,這個就能大顯身手。和post比較像,注意文件之前加一個@符號。


<?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 ();
//各種項設(shè)置,網(wǎng)上參考而來,可以查看php手冊,自己設(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 );
//容錯機制
if($return === false){
 var_dump(curl_error($ch));
 }
//curl_getinfo()獲取各種運行中信息,便于調(diào)試 
$info = curl_getinfo($ch);
echo "執(zhí)行時間".$info['total_time'].PHP_EOL;
//釋放
curl_close ( $ch );
print_r($return);


例五 : curl批處理。


curl有一個高級特性,批處理句柄。允許打開多個curl鏈接。


批處理就是打開多個curl句柄,并把這些句柄指派給一個批處理句柄,然后在while循環(huán)里等待處理完畢。curl_multi_exec()算是稱得上多線程處理,不過它還是屬于異步的范疇。

    

<?php
header("Content-type: text/html; charset=gbk");
$urls=array('http://www.baidu.com','http://www.qq.com/');
$ch=array();
//批處理句柄
$mh=curl_multi_init();
//打開多個curl句柄,并指派給一個批處理句柄
$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);//實現(xiàn)批處理,可以看做curl多線程,實際是異步范疇
}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);
?>

基本算是列舉了常用的一些實例。要想靈活運用curl,還是得熟悉curl的各個設(shè)置項,這些設(shè)置項才是curl的靈魂。


想要了解更多關(guān)于PHP開發(fā)方面內(nèi)容的小伙伴,請關(guān)注扣丁學(xué)堂PHP培訓(xùn)官網(wǎng)、微信等平臺,扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育有專業(yè)的PHP講師為您指導(dǎo),此外扣丁學(xué)堂老師精心推出的PHP視頻教程定能讓你快速掌握PHP從入門到精通開發(fā)實戰(zhàn)技能??鄱W(xué)堂PHP技術(shù)交流群:374332265。


扣丁學(xué)堂微信公眾號                          Python全棧開發(fā)爬蟲人工智能機器學(xué)習(xí)數(shù)據(jù)分析免費公開課直播間


【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】         【掃碼進入Python全棧開發(fā)免費公開課】



查看更多關(guān)于“php培訓(xùn)資訊”的相關(guān)文章>>


標(biāo)簽: PHP培訓(xùn) PHP基礎(chǔ)教程 PHP學(xué)習(xí)視頻 PHP教學(xué)視頻 PHP入門教程 PHP教程視頻 PHP在線學(xué)習(xí) PHP在線視頻 PHP在線教程 扣丁學(xué)堂PHP培訓(xùn)

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部