PHP的curl實(shí)現(xiàn)get和post
類似于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代理通道就最常用的來(lái)說(shuō),是基于http的get和post方法。
代碼實(shí)現(xiàn):
1、http的get實(shí)現(xiàn)
$ch = curl_init('http://www.devdao.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 postextract($_POST) ; //set POST variables$url = 'http://www.webjx.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 POSTforeach($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 connectioncurl_close($ch) ;
