php的curl實(shí)現(xiàn)get和post的代碼
更新時(shí)間:2008年08月23日 20:48:24 作者:
類(lèi)似于dreamhost這類(lèi)主機(jī)服務(wù)商,是顯示fopen的使用的。使用php的curl可以實(shí)現(xiàn)支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。
curl 支持SSL證書(shū)、HTTP POST、HTTP PUT 、FTP 上傳,kerberos、基于HTT格式的上傳、代理、cookie、用戶+口令證明、文件傳送恢復(fù)、http代理通道就最常用的來(lái)說(shuō),是基于http的get和post方法。
代碼實(shí)現(xiàn):
1、http的get實(shí)現(xiàn)
$ch = curl_init("http://www.dhdzp.com/") ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ;
$output = curl_exec($ch) ;
$fh = fopen("out.html", 'w') ;
fwrite($fh, $output) ;
fclose($fh) ;
2、http的post實(shí)現(xiàn)
//extract data from the post
extract($_POST) ;
//set POST variables
$url = 'http://www.dhdzp.com/get-post.php' ;
$fields = array(
'lname'=>urlencode($last_name) ,
'fname'=>urlencode($first_name) ,
'title'=>urlencode($title) ,
'company'=>urlencode($institution) ,
'age'=>urlencode($age) ,
'email'=>urlencode($email) ,
'phone'=>urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&' ; }
rtrim($fields_string ,'&') ;
//open connection
$ch = curl_init() ;
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL,$url) ;
curl_setopt($ch, CURLOPT_POST,count($fields)) ;
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string) ;
//execute post
$result = curl_exec($ch) ;
//close connection
curl_close($ch) ;
代碼實(shí)現(xiàn):
1、http的get實(shí)現(xiàn)
復(fù)制代碼 代碼如下:
$ch = curl_init("http://www.dhdzp.com/") ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ;
$output = curl_exec($ch) ;
$fh = fopen("out.html", 'w') ;
fwrite($fh, $output) ;
fclose($fh) ;
2、http的post實(shí)現(xiàn)
復(fù)制代碼 代碼如下:
//extract data from the post
extract($_POST) ;
//set POST variables
$url = 'http://www.dhdzp.com/get-post.php' ;
$fields = array(
'lname'=>urlencode($last_name) ,
'fname'=>urlencode($first_name) ,
'title'=>urlencode($title) ,
'company'=>urlencode($institution) ,
'age'=>urlencode($age) ,
'email'=>urlencode($email) ,
'phone'=>urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&' ; }
rtrim($fields_string ,'&') ;
//open connection
$ch = curl_init() ;
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL,$url) ;
curl_setopt($ch, CURLOPT_POST,count($fields)) ;
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string) ;
//execute post
$result = curl_exec($ch) ;
//close connection
curl_close($ch) ;
您可能感興趣的文章:
- PHP中使用cURL實(shí)現(xiàn)Get和Post請(qǐng)求的方法
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- 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請(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)求類(lèi)型】
相關(guān)文章
如何用PHP websocket實(shí)現(xiàn)網(wǎng)頁(yè)實(shí)時(shí)聊天
websocket作為HTML5里一個(gè)新的特性一直很受人關(guān)注,因?yàn)樗娴姆浅??,打破了http“請(qǐng)求-響應(yīng)”的常規(guī)思維,實(shí)現(xiàn)了服務(wù)器向客戶端主動(dòng)推送消息,本文介紹如何使用PHP和JS應(yīng)用websocket實(shí)現(xiàn)一個(gè)網(wǎng)頁(yè)實(shí)時(shí)聊天室。2021-05-05
PHP實(shí)現(xiàn)微信模擬登陸并給用戶發(fā)送消息的方法【文字,圖片,圖文】
這篇文章主要介紹了PHP實(shí)現(xiàn)微信模擬登陸并給用戶發(fā)送消息的方法,可實(shí)現(xiàn)發(fā)送文字、圖片及圖文的功能,涉及php針對(duì)微信接口的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
php實(shí)現(xiàn)阿拉伯?dāng)?shù)字和羅馬數(shù)字相互轉(zhuǎn)換的方法
這篇文章主要介紹了php實(shí)現(xiàn)阿拉伯?dāng)?shù)字和羅馬數(shù)字相互轉(zhuǎn)換的方法,涉及php字符串操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
php獲取當(dāng)前時(shí)間的毫秒數(shù)的方法
php本身沒(méi)有提供返回毫秒數(shù)的函數(shù),但提供了一個(gè)microtime()函數(shù),借助此函數(shù),可以很容易定義一個(gè)返回毫秒數(shù)的函數(shù)2014-01-01
php+mysql實(shí)現(xiàn)簡(jiǎn)單的增刪改查功能
本文給大家分享的是使用php結(jié)合mysql實(shí)現(xiàn)簡(jiǎn)單的增刪改查的功能的代碼,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-07-07
PHP編程獲取各個(gè)時(shí)間段具體時(shí)間的方法
這篇文章主要介紹了PHP編程獲取各個(gè)時(shí)間段具體時(shí)間的方法,結(jié)合實(shí)例形式分析了基于date與strtotime函數(shù)進(jìn)行日期時(shí)間運(yùn)算的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
php7基于遞歸實(shí)現(xiàn)刪除空文件夾的方法示例
這篇文章主要介紹了php7基于遞歸實(shí)現(xiàn)刪除空文件夾的方法,結(jié)合具體實(shí)例分析了php7遞歸遍歷目錄及判斷、刪除等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
利用php獲取服務(wù)器時(shí)間的實(shí)現(xiàn)代碼
本篇文章是對(duì)使用php獲取服務(wù)器時(shí)間的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

