解析curl提交GET,POST,Cookie的簡單方法_PHP教程
推薦:解析PHP中empty is_null和isset的測試代碼如下: 復(fù)制代碼 代碼如下: ?php $a; $b = false; $c = ''; $d = 0; $e = null; $f = array(); 首先是empty的var_dump輸出: boolean true boolean true boolean true boolean true boolean true boolean true 然后是is_null的輸出: boolean true boolean false bool
本篇文章是對curl提交GET,POST,Cookie的簡單方法進行了詳細的分析介紹,需要的朋友參考下 復(fù)制代碼 代碼如下:
<?php
$get_data = array (
"get1"=> "get1",
"get2" => "get2",
"get3" => "get3"
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://test.test.com/test.php?'.http_build_query($get_data));
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$post_data = array (
"p1" => "test1",
"p2" => "test2",
"p3" => "test3"
);
curl_setopt($curl, CURLOPT_POST, true);
//["CONTENT_TYPE"]=> string(70) "multipart/form-data; boundary=------077a996f5afe"
//要發(fā)送文件,在文件名前面加上@前綴并使用完整路徑。
//使用數(shù)組提供post數(shù)據(jù)時,CURL組件大概是為了兼容@filename這種上傳文件的寫法,默認把content_type設(shè)為了multipart/form-data。
//雖然對于大多數(shù)web服務(wù)器并沒有影響,但是還是有少部分服務(wù)器不兼容。
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
//["CONTENT_TYPE"]=> string(33) "application/x-www-form-urlencoded"
//curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
//在沒有需要上傳文件的情況下,盡量對post提交的數(shù)據(jù)進行http_build_query,然后發(fā)送出去,能實現(xiàn)更好的兼容性,更小的請求數(shù)據(jù)包。
$cookies = array(
'c1'=>'v1',
'c2'=>'v2',
'c3'=>'v3',
);
$cookies_string = '';
foreach($cookies as $name=>$value){
$cookies_string .= $name.'='.$value.';';
}
curl_setopt($curl, CURLOPT_COOKIE, $cookies_string);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
exit;
分享:php selectradio和checkbox默認選擇的實現(xiàn)方法詳解本篇文章是對php selectradio和checkbox默認選擇的實現(xiàn)方法進行了詳細的分析介紹,需要的朋友參考下 這是擴展yibing的select默認選擇的實現(xiàn)方法 復(fù)制代碼 代碼如下: select name=wuyeleixing size=1 option ?php if($myrow[wuyeleixing]==1) echo(selected);? value=1住
- 相關(guān)鏈接:
- 教程說明:
PHP教程-解析curl提交GET,POST,Cookie的簡單方法
。