色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

PHP的curl實(shí)現(xiàn)get,post和cookie(實(shí)例介紹)

類似于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代理通道就最常用的來說,是基于http的 get和post方法。
代碼實(shí)現(xiàn):
1、http的get實(shí)現(xiàn)
復(fù)制代碼 代碼如下:
$ch = curl_init("http://www.domain.com/api/index.php?test=1") ; 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 獲取數(shù)據(jù)返回 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在啟用 CURLOPT_RETURNTRANSFER 時(shí)候?qū)@取數(shù)據(jù)返回 
echo $output = curl_exec($ch) ; 

/* 寫入文件 */ 
$fh = fopen("out.html", 'w') ; 
fwrite($fh, $output) ; 
fclose($fh) ;  

2、http的post實(shí)現(xiàn)
復(fù)制代碼 代碼如下:
<?php 
$url = 'http://www.domain.com/api/' ; 
$fields = array( 
               'lname'=>'justcoding' , 
               'fname'=>'phplover' , 
               'title'=>'myapi', 
               'age'=>'27' , 
               'email'=>'1353777303@gmail.com' , 
               'phone'=>'1353777303' 
              ); 
//$post_data = implode('&',$fields); 

注意:post請(qǐng)求的參數(shù)要用get方式那樣連接起來,作為字符串傳遞:
如:$params = 'userId='.$this->user_id.'&auth='.$this->auth.'&sig='.$this->sig

還有跨平臺(tái)的請(qǐng)求,curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自動(dòng)跳轉(zhuǎn) (很重要)

//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)) ; // 啟用時(shí)會(huì)發(fā)送一個(gè)常規(guī)的POST請(qǐng)求,類型為:application/x-www-form-urlencoded,就像表單提交的一樣。 
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields); // 在HTTP中的“POST”操作。如果要傳送一個(gè)文件,需要一個(gè)@開頭的文件名 

ob_start(); 
curl_exec($ch); 
$result = ob_get_contents() ; 
ob_end_clean(); 

echo $result; 

//close connection 
curl_close($ch) ; 

復(fù)制代碼 代碼如下:
<?php 

if($_GET['test']) 

     print_r($_GET); 


if($_POST) 

    print_r($_POST); 
}  

php的curl傳送cookie
 
兩種方式:
一種是自動(dòng):
復(fù)制代碼 代碼如下:
curl_setopt($curlHandle, CURLOPT_COOKIEJAR, 'cookie.txt '); //保存 
curl_setopt($curlHandle, CURLOPT_COOKIEFILE, 'cookie.txt '); //讀取 

這樣COOKIE會(huì)自動(dòng)跟上去.
不過要分兩次,一是先訪問產(chǎn)生cookie,接著連結(jié)才能用cookie
例子:
復(fù)制代碼 代碼如下:
<?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); 

   //設(shè)置文件讀取并提交的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"; //設(shè)置POST參數(shù) 
   //curl_setopt($ch, CURLOPT_POSTFIELDS, $request);    
   // 上面這句,當(dāng)然你可以說你是baidu,改掉這里的值就ok了,可以實(shí)現(xiàn)小偷的功能,$_SERVER['HTTP_USER_AGENT'] 
   //你也可以自己做個(gè) spider 了,那么就偽裝這里的 CURLOPT_USERAGENT 吧 
   //如果你要把這個(gè)程序放到linux上用php -q執(zhí)行那也要寫出具體的$_SERVER['HTTP_USER_AGENT'],偽造的也可以 
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
   curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar); 
   curl_setopt($ch, CURLOPT_HEADER, false);//設(shè)定是否輸出頁面內(nèi)容 
   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; 


?> 

一種自定義:
復(fù)制代碼 代碼如下:
$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: '.$你的目標(biāo)HOST; 
$header[]= 'Connection: Keep-Alive '; 
$header[]= 'Cookie: '.$你的COOKIE串; 

curl_setopt($curlHandel,CURLOPT_HTTPHEADER,$header); 

php技術(shù)PHP的curl實(shí)現(xiàn)get,post和cookie(實(shí)例介紹),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 克什克腾旗| 临武县| 台湾省| 五华县| 开封市| 九龙县| 福州市| 冀州市| 高台县| 达日县| 东乌珠穆沁旗| 定南县| 新宾| 徐汇区| 宜黄县| 新巴尔虎左旗| 宁南县| 万盛区| 枣阳市| 乌拉特前旗| 友谊县| 祥云县| 伊吾县| 尖扎县| 渝中区| 湘阴县| 高要市| 冕宁县| 黄冈市| 太仆寺旗| 呼和浩特市| 花莲市| 垫江县| 上栗县| 云梦县| 溧阳市| 吉首市| 衡东县| 和平区| 云霄县| 新龙县|