- 相關推薦
php的curl實現get和post的代碼
類似于dreamhost這類主機服務商,是顯示fopen的使用的。使用php的curl可以實現支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。具體使用如下,更多消息請關注應屆畢業生網!
curl 支持SSL證書、HTTP POST、HTTP PUT 、FTP 上傳,kerberos、基于HTT格式的上傳、代理、cookie、用戶+口令證明、文件傳送恢復、http代理通道就最常用的來說,是基于http的get和post方法。
代碼實現:
1、http的get實現
代碼如下:
?
$ch = curl_init("http://www.jb51.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實現
代碼如下:
?
//extract data from the post
extract($_POST) ;
//set POST variables
$url = 'http://www.jb51.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 datacurl_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實現get和post的代碼】相關文章:
PHP中使用cURL實現Get和Post請求的方法05-23
PHP的GET和POST請求發送方法04-24
php的curl模擬post請求的例子12-17
php中curl模擬post請求小實例06-01