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.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實現


//extract data from the post
extract($_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 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) ;

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *