php的curl實現(xiàn)get和post的代碼
更新時間:2008年08月23日 20:48:24 作者:
類似于dreamhost這類主機服務(wù)商,是顯示fopen的使用的。使用php的curl可以實現(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方法。
代碼實現(xiàn):
1、http的get實現(xiàn)
$ch = curl_init("http://m.fzitv.net/") ;
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實現(xiàn)
//extract data from the post
extract($_POST) ;
//set POST variables
$url = 'http://m.fzitv.net/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) ;
代碼實現(xiàn):
1、http的get實現(xiàn)
復(fù)制代碼 代碼如下:
$ch = curl_init("http://m.fzitv.net/") ;
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實現(xiàn)
復(fù)制代碼 代碼如下:
//extract data from the post
extract($_POST) ;
//set POST variables
$url = 'http://m.fzitv.net/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實現(xiàn)Get和Post請求的方法
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- PHP中的使用curl發(fā)送請求(GET請求和POST請求)
- PHP的curl實現(xiàn)get,post和cookie(實例介紹)
- 詳解php用curl調(diào)用接口方法,get和post兩種方式
- php使用CURL模擬GET與POST向微信接口提交及獲取數(shù)據(jù)的方法
- PHP CURL模擬GET及POST函數(shù)代碼
- PHP如何使用cURL實現(xiàn)Get和Post請求
- PHP中使用CURL發(fā)送get/post請求上傳圖片批處理功能
- php curl發(fā)起get與post網(wǎng)絡(luò)請求案例詳解
- PHP curl get post 請求的封裝函數(shù)示例【get、post、put、delete等請求類型】
相關(guān)文章
如何用PHP websocket實現(xiàn)網(wǎng)頁實時聊天
websocket作為HTML5里一個新的特性一直很受人關(guān)注,因為它真的非常酷,打破了http“請求-響應(yīng)”的常規(guī)思維,實現(xiàn)了服務(wù)器向客戶端主動推送消息,本文介紹如何使用PHP和JS應(yīng)用websocket實現(xiàn)一個網(wǎng)頁實時聊天室。2021-05-05
PHP實現(xiàn)微信模擬登陸并給用戶發(fā)送消息的方法【文字,圖片,圖文】
這篇文章主要介紹了PHP實現(xiàn)微信模擬登陸并給用戶發(fā)送消息的方法,可實現(xiàn)發(fā)送文字、圖片及圖文的功能,涉及php針對微信接口的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
php實現(xiàn)阿拉伯?dāng)?shù)字和羅馬數(shù)字相互轉(zhuǎn)換的方法
這篇文章主要介紹了php實現(xiàn)阿拉伯?dāng)?shù)字和羅馬數(shù)字相互轉(zhuǎn)換的方法,涉及php字符串操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04

