php的curl實(shí)現(xiàn)get和post的代碼
更新時(shí)間:2008年08月23日 20:48:24 作者:
類似于dreamhost這類主機(jī)服務(wù)商,是顯示fopen的使用的。使用php的curl可以實(shí)現(xiàn)支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。
curl 支持SSL證書、HTTP POST、HTTP PUT 、FTP 上傳,kerberos、基于HTT格式的上傳、代理、cookie、用戶+口令證明、文件傳送恢復(fù)、http代理通道就最常用的來說,是基于http的get和post方法。
代碼實(shí)現(xiàn):
1、http的get實(shí)現(xiàn)
$ch = curl_init("http://www.dbjr.com.cn/") ;
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.dbjr.com.cn/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.dbjr.com.cn/") ;
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.dbjr.com.cn/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)求類型】
相關(guān)文章
用php實(shí)現(xiàn)像JSP,ASP里Application那樣的全局變量
用php實(shí)現(xiàn)像JSP,ASP里Application那樣的全局變量...2007-01-01詳解phpmyadmin相關(guān)配置與錯(cuò)誤解決
這篇文章主要介紹了詳解phpmyadmin相關(guān)配置與錯(cuò)誤解決的相關(guān)資料,希望通過本文能幫助到大家,讓大家遇到這種問題可以解決,需要的朋友可以參考下2017-10-10php實(shí)現(xiàn)的click captcha點(diǎn)擊驗(yàn)證碼類實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)的click captcha點(diǎn)擊驗(yàn)證碼類實(shí)例,不同于以往傳統(tǒng)的驗(yàn)證碼,該驗(yàn)證碼類可實(shí)現(xiàn)手機(jī)用戶點(diǎn)擊某一位置確認(rèn)驗(yàn)證碼,非常實(shí)用,需要的朋友可以參考下2014-09-09PHP實(shí)現(xiàn)統(tǒng)計(jì)一個(gè)數(shù)字在排序數(shù)組中出現(xiàn)次數(shù)的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)統(tǒng)計(jì)一個(gè)數(shù)字在排序數(shù)組中出現(xiàn)次數(shù)的方法,涉及php基于二分查找算法在數(shù)組中進(jìn)行查找及統(tǒng)計(jì)的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01