PHP的curl實現(xiàn)get,post和cookie(實例介紹)
類似于dreamhost這類主機服務商,是顯示fopen的使用 的。使用php的curl可以實現(xiàn)支持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方法。
代碼實現(xiàn):
1、http的get實現(xiàn)
$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 時候?qū)@取數(shù)據(jù)返回
echo $output = curl_exec($ch) ;
/* 寫入文件 */
$fh = fopen("out.html", 'w') ;
fwrite($fh, $output) ;
fclose($fh) ;
2、http的post實現(xiàn)
<?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請求的參數(shù)要用get方式那樣連接起來,作為字符串傳遞:
如:$params = 'userId='.$this->user_id.'&auth='.$this->auth.'&sig='.$this->sig
還有跨平臺的請求,curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉(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)) ; // 啟用時會發(fā)送一個常規(guī)的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) ;
<?php
if($_GET['test'])
{
print_r($_GET);
}
if($_POST)
{
print_r($_POST);
}
php的curl傳送cookie
兩種方式:
一種是自動:
curl_setopt($curlHandle, CURLOPT_COOKIEJAR, 'cookie.txt '); //保存
curl_setopt($curlHandle, CURLOPT_COOKIEFILE, 'cookie.txt '); //讀取
這樣COOKIE會自動跟上去.
不過要分兩次,一是先訪問產(chǎn)生cookie,接著連結(jié)才能用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參數(shù)
//curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
// 上面這句,當然你可以說你是baidu,改掉這里的值就ok了,可以實現(xiàn)小偷的功能,$_SERVER['HTTP_USER_AGENT']
//你也可以自己做個 spider 了,那么就偽裝這里的 CURLOPT_USERAGENT 吧
//如果你要把這個程序放到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);//設定是否輸出頁面內(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;
}
?>
一種自定義:
$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實現(xiàn)Get和Post請求的方法
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- php的curl實現(xiàn)get和post的代碼
- PHP中的使用curl發(fā)送請求(GET請求和POST請求)
- 詳解php用curl調(diào)用接口方法,get和post兩種方式
- php使用CURL模擬GET與POST向微信接口提交及獲取數(shù)據(jù)的方法
- PHP CURL模擬GET及POST函數(shù)代碼
- PHP如何使用cURL實現(xiàn)Get和Post請求
- PHP中使用CURL發(fā)送get/post請求上傳圖片批處理功能
- php curl發(fā)起get與post網(wǎng)絡請求案例詳解
- PHP curl get post 請求的封裝函數(shù)示例【get、post、put、delete等請求類型】
相關(guān)文章
PHP調(diào)用Twitter的RSS的實現(xiàn)代碼
“守望軒”博客右側(cè)邊欄原來有個“雜感”的欄目,用來記錄短的、不能大篇幅成文的短句,或者自己比較喜歡的短句和言論。2010-03-03PHP中信息格式化操作詳解(MessageFormatter類)
這篇文章主要給大家介紹了關(guān)于PHP中信息格式化操作的相關(guān)資料,主要運用的是專門用于信息格式化的MessageFormatter類,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-07-07通過PHP current函數(shù)獲取未知字符鍵名數(shù)組第一個元素的值
在開發(fā)中經(jīng)常遇到這樣問題,獲取數(shù)組第一個元素的值,如果是數(shù)字索引那還好,直接$array[0],如果鍵名是字符串,你又未知這個字符串呢?用current()函數(shù)就可以做到2013-06-06