PHP的curl實現get,post 和 cookie

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   ar   strong   

類似於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代碼 

$ch = curl_init("http://www.domain.com/api/index.php?test=1") ;  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 擷取資料返回  curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在啟用 CURLOPT_RETURNTRANSFER 時候將擷取資料返回  echo $output = curl_exec($ch) ;    /* 寫入檔案 */  $fh = fopen("out.html", ‘w‘) ;  fwrite($fh, $output) ;  fclose($fh) ;  

2、http的post實現

    <?php      $url = ‘http://www.domain.com/api/‘ ;      $fields = array(                     ‘lname‘=>‘justcoding‘ ,                     ‘fname‘=>‘phplover‘ ,                     ‘title‘=>‘myapi‘,                     ‘age‘=>‘27‘ ,                     ‘email‘=>‘[email protected]‘ ,                     ‘phone‘=>‘1353777303‘                    );      //$post_data = implode(‘&‘,$fields);            //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)) ; // 啟用時會發送一個常規的POST請求,類型為:application/x-www-form-urlencoded,就像表單提交的一樣。      curl_setopt($ch, CURLOPT_POSTFIELDS,$fields); // 在HTTP中的“POST”操作。如果要傳送一個檔案,需要一個@開頭的檔案名稱            ob_start();      curl_exec($ch);      $result = ob_get_contents() ;      ob_end_clean();            echo $result;            //close connection      curl_close($ch) ;  

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

 

    <?php            if($_GET[‘test‘])      {           print_r($_GET);      }            if($_POST)      {          print_r($_POST);      }   

3. php的curl傳送cookie

    curl_setopt($curlHandle, CURLOPT_COOKIEJAR, ‘cookie.txt ‘); //儲存      curl_setopt($curlHandle, CURLOPT_COOKIEFILE, ‘cookie.txt ‘); //讀取  

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

例子:

    <?php               function get_curlcuconent2($filename,$referer)      {         $cookie_jar = tempnam(‘./tmp‘,‘JSESSIONID‘);                      $ch = curl_init();         curl_setopt($ch, CURLOPT_URL, $filename);         curl_setopt($ch, CURLOPT_HEADER, false);         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);               //設定檔案讀取並提交的cookie路徑         curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);         $filecontent=curl_exec($ch);         curl_close($ch);                      $ch = curl_init();         $hostname ="www.domain.com";         //$referer="http://www.domain.com/";         curl_setopt($ch, CURLOPT_URL, $filename);         curl_setopt($ch, CURLOPT_REFERER, $referer); // 看這裡,你也可以說你從google來         curl_setopt($ch, CURLOPT_USERAGENT, "www.domain.com");               //$request = "JSESSIONID=abc6szw15ozvZ_PU9b-8r"; //設定POST參數         //curl_setopt($ch, CURLOPT_POSTFIELDS, $request);            // 上面這句,當然你可以說你是baidu,改掉這裡的值就ok了,可以實現小偷的功能,$_SERVER[‘HTTP_USER_AGENT‘]         //你也可以自己做個 spider 了,那麼就偽裝這裡的 CURLOPT_USERAGENT 吧         //如果你要把這個程式放到linux上用php -q執行那也要寫出具體的$_SERVER[‘HTTP_USER_AGENT‘],偽造的也可以         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);         curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);         curl_setopt($ch, CURLOPT_HEADER, false);//設定是否輸出頁面內容         curl_setopt($ch, CURLOPT_GET, 1); // post,get 過去               $filecontent = curl_exec($ch);         preg_match_all("/charset=(.+?)[NULL\"\‘]/is",$filecontent, $charsetarray);         if(strtolower($charsetarray[1][0])=="utf-8")               $filecontent=iconv( ‘utf-8‘, ‘gb18030//IGNORE‘ , $filecontent);         curl_close($ch);         return $filecontent;      }            ?>  

一種自訂:

    $header[]= ‘Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, text/html, * ‘. ‘/* ‘;      $header[]= ‘Accept-Language: zh-cn ‘;      $header[]= ‘User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) ‘;      $header[]= ‘Host: ‘.$你的目標HOST;      $header[]= ‘Connection: Keep-Alive ‘;      $header[]= ‘Cookie: ‘.$你的COOKIE串;            curl_setopt($curlHandel,CURLOPT_HTTPHEADER,$header);   

 

PHP的curl實現get,post 和 cookie

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.