PHP的curl實現get,post 和 cookie

類似於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實現

 

Php代碼  收藏代碼
  1. $ch = curl_init("http://www.domain.com/api/index.php?test=1") ;  
  2. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 獲取數據返回  
  3. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在啓用 CURLOPT_RETURNTRANSFER 時候將獲取數據返回  
  4. echo $output = curl_exec($ch) ;  
  5.   
  6. /* 寫入文件 */  
  7. $fh = fopen("out.html"'w') ;  
  8. fwrite($fh$output) ;  
  9. fclose($fh) ;   


2、http的post實現

 

Php代碼  收藏代碼
  1. <?php  
  2. $url = 'http://www.domain.com/api/' ;  
  3. $fields = array(  
  4.                'lname'=>'justcoding' ,  
  5.                'fname'=>'phplover' ,  
  6.                'title'=>'myapi',  
  7.                'age'=>'27' ,  
  8.                'email'=>'[email protected]' ,  
  9.                'phone'=>'1353777303'  
  10.               );  
  11. //$post_data = implode('&',$fields);  
  12.   
  13. //open connection  
  14. $ch = curl_init() ;  
  15. //set the url, number of POST vars, POST data  
  16. curl_setopt($ch, CURLOPT_URL,$url) ;  
  17. curl_setopt($ch, CURLOPT_POST,count($fields)) ; // 啓用時會發送一個常規的POST請求,類型爲:application/x-www-form-urlencoded,就像表單提交的一樣。  
  18. curl_setopt($ch, CURLOPT_POSTFIELDS,$fields); // 在HTTP中的“POST”操作。如果要傳送一個文件,需要一個@開頭的文件名  
  19.   
  20. ob_start();  
  21. curl_exec($ch);  
  22. $result = ob_get_contents() ;  
  23. ob_end_clean();  
  24.   
  25. echo $result;  
  26.   
  27. //close connection  
  28. curl_close($ch) ;  
 

http://www.domain.com/api/index.php

 

Php代碼  收藏代碼
  1. <?php  
  2.   
  3. if($_GET['test'])  
  4. {  
  5.      print_r($_GET);  
  6. }  
  7.   
  8. if($_POST)  
  9. {  
  10.     print_r($_POST);  
  11. }   

 

3. php的curl傳送cookie

 

兩種方式:

 

一種是自動:

 

Php代碼  收藏代碼
  1. curl_setopt($curlHandle, CURLOPT_COOKIEJAR, 'cookie.txt '); //保存  
  2. curl_setopt($curlHandle, CURLOPT_COOKIEFILE, 'cookie.txt '); //讀取  
 

這樣COOKIE會自動跟上去. 
不過要分兩次,一是先訪問產生cookie,接着連結才能用cookie

 

例子:

 

Php代碼  收藏代碼
  1. <?php     
  2.   
  3. function get_curlcuconent2($filename,$referer)  
  4. {  
  5.    $cookie_jar = tempnam('./tmp','JSESSIONID');  
  6.          
  7.    $ch = curl_init();  
  8.    curl_setopt($ch, CURLOPT_URL, $filename);  
  9.    curl_setopt($ch, CURLOPT_HEADER, false);  
  10.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  11.   
  12.    //設置文件讀取並提交的cookie路徑  
  13.    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);  
  14.    $filecontent=curl_exec($ch);  
  15.    curl_close($ch);  
  16.          
  17.    $ch = curl_init();  
  18.    $hostname ="www.domain.com";  
  19.    //$referer="http://www.domain.com/";  
  20.    curl_setopt($ch, CURLOPT_URL, $filename);  
  21.    curl_setopt($ch, CURLOPT_REFERER, $referer); // 看這裏,你也可以說你從google來  
  22.    curl_setopt($ch, CURLOPT_USERAGENT, "www.domain.com");  
  23.   
  24.    //$request = "JSESSIONID=abc6szw15ozvZ_PU9b-8r"; //設置POST參數  
  25.    //curl_setopt($ch, CURLOPT_POSTFIELDS, $request);     
  26.    // 上面這句,當然你可以說你是baidu,改掉這裏的值就ok了,可以實現小偷的功能,$_SERVER['HTTP_USER_AGENT']  
  27.    //你也可以自己做個 spider 了,那麼就僞裝這裏的 CURLOPT_USERAGENT 吧  
  28.    //如果你要把這個程序放到linux上用php -q執行那也要寫出具體的$_SERVER['HTTP_USER_AGENT'],僞造的也可以  
  29.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  30.    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);  
  31.    curl_setopt($ch, CURLOPT_HEADER, false);//設定是否輸出頁面內容  
  32.    curl_setopt($ch, CURLOPT_GET, 1); // post,get 過去  
  33.   
  34.    $filecontent = curl_exec($ch);  
  35.    preg_match_all("/charset=(.+?)[NULL\"\']/is",$filecontent$charsetarray);  
  36.    if(strtolower($charsetarray[1][0])=="utf-8")  
  37.          $filecontent=iconv( 'utf-8''gb18030//IGNORE' , $filecontent);  
  38.    curl_close($ch);  
  39.    return $filecontent;  
  40. }  
  41.   
  42. ?>  
 


一種自定義:

 

Php代碼  收藏代碼
  1. $header[]= 'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, text/html, * ''/* ';  
  2. $header[]= 'Accept-Language: zh-cn ';  
  3. $header[]= 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) ';  
  4. $header[]= 'Host: '.$你的目標HOST;  
  5. $header[]= 'Connection: Keep-Alive ';  
  6. $header[]= 'Cookie: '.$你的COOKIE串;  
  7.   
  8. curl_setopt($curlHandel,CURLOPT_HTTPHEADER,$header);   

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章