php請求post
① php實現模擬post請求用法實例
本文實例講述了php實現模擬post請求的方法。分享給大家供大家參考。具體如下:
class
Request{
public
static
function
post($url,
$post_data
=
'',
$timeout
=
5){//curl
$ch
=
curl_init();
curl_setopt
($ch,
CURLOPT_URL,
$url);
curl_setopt
($ch,
CURLOPT_POST,
1);
if($post_data
!=
''){
curl_setopt($ch,
CURLOPT_POSTFIELDS,
$post_data);
}
curl_setopt
($ch,
CURLOPT_RETURNTRANSFER,
1);
curl_setopt
($ch,
CURLOPT_CONNECTTIMEOUT,
$timeout);
curl_setopt($ch,
CURLOPT_HEADER,
false);
$file_contents
=
curl_exec($ch);
curl_close($ch);
return
$file_contents;
}
public
static
function
post2($url,
$data=array()){//file_get_content
$postdata
=
http_build_query(
$data
);
$opts
=
array('http'
=>
array(
'method'
=>
'POST',
'header'
=>
'Content-type:
application/x-www-form-urlencoded',
'content'
=>
$postdata
)
);
$context
=
stream_context_create($opts);
$result
=
file_get_contents($url,
false,
$context);
return
$result;
}
public
static
function
post3($host,$path,$query,$others=''){//fsocket
$post="POST
$path
HTTP/1.1\r\nHost:
$host\r\n";
$post.="Content-type:
application/x-www-form-";
$post.="urlencoded\r\n${others}";
$post.="User-Agent:
Mozilla
4.0\r\nContent-length:
";
$post.=strlen($query)."\r\nConnection:
close\r\n\r\n$query";
$h=fsockopen($host,80);
fwrite($h,$post);
for($a=0,$r='';!$a;){
$b=fread($h,8192);
$r.=$b;
$a=(($b=='')?1:0);
}
fclose($h);
return
$r;
}
}
$url='http://******/con/Inter.php';
$data=Request::post($url,array('api'=>'tag_list'));
$data2=Request::post2($url,array('api'=>'tag_list'));
echo
$data;
希望本文所述對大家的php程序設計有所幫助。
② PHP 模擬HTTP發送POST請求
用php模擬登陸主要分為三部分1. post數據。2.根據返回的http頭,從中截出cookie段。3.偽造http頭發送請求。 我這里以用php抓取163相冊的需要密碼才能訪問的目錄為例。<?phpfunction posttohost($url, $data) //post數據if (!isset($url['query'])) $encoded = "";foreach ($data as $k=>$v) $fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);if (!$fp) return "Failed to open socket to $url[host]";fputs($fp, sprintf("POST %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? "?" : "", $url['query']));fputs($fp, "Host: $url[host]\n");fputs($fp, "Content-type: application/x-www-form-urlencoded\n");fputs($fp, "Content-length: " . strlen($encoded) . "\n");fputs($fp, "Connection: close\n\n");fputs($fp, "$encoded\n");$line = fgets($fp,1024);if (!eregi("^HTTP/1\.. 200", $line)) return;$results = ""; $inheader = 1;while(!feof($fp)) elseif ($inheader) }fclose($fp);return $results;} 答案補充 function getjs($juser,$jaid,$jcookie) //偽造http頭 答案補充 else fclose( $socket ); return $ret;}}$iurl=' http://photo.163.com/photos/'.$iuser.'/'.$aid.'/';$idata=array ('pass'=>$pass,'checking'=>'1'); //通過winsock抓包發現,輸入訪問密碼,會向163相冊發送 pass=密碼&checking=1$mystr=posttohost($iurl,$idata);$pattern='/HALFORDER=(.*?);/';preg_match($pattern,$mystr,$out);$str=getjs($iuser,$aid,$out[1]);echo $str;?>
③ 關於PHP中POST傳遞參數問題
將數據轉換成 json 格式的字元串, 並通過 CURL 的 POST 的形式傳遞參數給服務端, 但是在服務端無法用 $_POST 獲取到數據。後台用 $_POST 獲取到的信息為空, 但是可以通過 $post = file_get_contents("php://input") 獲取到請求的相關信息。
Coentent-Type 的值為 application/x-www-data-urlencode 和 multipart/form-data 時, php才會將http請求數據包中的數據填進 $_POST 。
如果 POST 的原始數據是一維數組或&拼接的標准格式的鍵值對字元串,那麼可以用 $_POST 來獲取。
如果要通過 file_get_contents 獲取,這種情況下可以發送 json 字元串,用 json_encode 編碼轉換一下,或者使用 http_build_query 。
1、 區別 PHP 的 $_POST、$HTTP_RAW_POST_DATA 和 php://input
2、 accept 和 content-Type區別
3、 Http Header里的Content-Type
④ php怎樣判斷當前請求是post還是get
具體的判斷方法如下:
⑤ 怎麼用PHP發送POST請求
PHP發送POST請求的三種方式
classRequest{
publicstaticfunctionpost($url,$post_data='',$timeout=5){//curl
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
if($post_data!=''){
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
}
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch,CURLOPT_HEADER,false);
$file_contents=curl_exec($ch);
curl_close($ch);
return$file_contents;
}
publicstaticfunctionpost2($url,$data){//file_get_content$postdata=http_build_query(
$data
);$opts=array('http'=>
array(
'method'=>'POST',
'header'=>'Content-type:application/x-www-form-urlencoded',
'content'=>$postdata
)
);$context=stream_context_create($opts);
$result=file_get_contents($url,false,$context);
return$result;
}
publicstaticfunctionpost3($host,$path,$query,$others=''){//fsocket
$post="POST$pathHTTP/1.1
Host:$host
";
$post.="Content-type:application/x-www-form-";
$post.="urlencoded
${others}";
$post.="User-Agent:Mozilla4.0
Content-length:";
$post.=strlen($query)."
Connection:close
$query";
$h=fsockopen($host,80);
fwrite($h,$post);
for($a=0,$r='';!$a;){
$b=fread($h,8192);
$r.=$b;
$a=(($b=='')?1:0);
}
fclose($h);
return$r;
}
}http://www.oschina.net/code/snippet_729516_33065
⑥ PHP中怎樣發送post請求並獲取網頁
$post='POST數據';
//初始化
$curl=curl_init('URL');
$header=array();
$header[]='User-Agent:Mozilla/5.0(WindowsNT6.1)AppleWebKit/537.36(KHTML,likeGecko)Chrome/42.0.2311.90Safari/537.36';
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
//不輸出header頭信息
curl_setopt($curl,CURLOPT_HEADER,0);
//保存到字元串而不是輸出
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
//post數據
curl_setopt($curl,CURLOPT_POST,1);
//請求數據
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
//是否抓取跳轉後的頁面
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
$response=curl_exec($curl);
curl_close($curl);
echo$response;
⑦ PHP。POST請求的問題
最簡單的話就是使用session保存,其次可以把數據存儲在資料庫里,或者文件里,然後在register.php里查詢。
原生session使用方法
session_start();
//賦值
$_SESSION["Session名稱"]=變數或字元串信息;
//使用
$_SESSION["Session名稱"];
⑧ php 怎麼POST獲取數據
方法1、最常見的方法是:$_post['fieldname'];
說明:只能接收content-type:
application/x-www-form-urlencoded提交的數據
解釋:也就是表單post過來的數據
方法2、file_get_contents("php://input");
說明:
允許讀取
post
的原始數據。
和
$http_raw_post_data
比起來,它給內存帶來的壓力較小,並且不需要任何特殊的
php.ini
設置。
php://input
不能用於
enctype="multipart/form-data"。
解釋:
對於未指定
content-type
的post數據,則可以使用file_get_contents(「php://input」);來獲取原始數據。
事實上,用php接收post的任何數據都可以使用本方法。而不用考慮content-type,包括二進制文件流也可以。
所以用方法二是最保險的方法
⑨ php發送get,post請求的幾種方法
POST方法比較適合用於發送一個保密的(比如信用卡號)或者比較大量的數據到伺服器,但速度慢。而Get方法會將所要傳輸的數據附在網址後面,然後一起送達伺服器,因此傳送的數據量就會受到限制且不安全,但是執行效率卻比 Post方法好。具體來說如下:
1、Get將表單中數據的按照variable=value的形式,添加到action所指向的URL後面,並且兩者使用「?」連接,而各個變數之間使用「&」連接;Post是將表單中的數據放在form的數據體中,按照變數和值相對應的方式,傳遞到action所指向URL。
2、Get是不安全的,因為在傳輸過程,數據被放在請求的URL中,而如今現有的很多伺服器、代理伺服器或者用戶代理都會將請求URL記錄到日誌文件中,然後放在某個地方,這樣就可能會有一些隱私的信息被第三方看到。另外,用戶也可以在瀏覽器上直接看到提交的數據,一些系統內部消息將會一同顯示在用戶面前。Post的所有操作對用戶來說都是不可見的。
3、Get傳輸的數據量小,這主要是因為受URL長度限制;而Post可以傳輸大量的數據,所以在上傳文件只能使用Post(當然還有一個原因,將在後面的提到)。
4、Get限制Form表單的數據集的值必須為ASCII字元;而Post支持整個ISO10646字元集。
5、Get是Form的默認方法。
