本文目錄一覽:
- 1、php實現模擬post請求用法實例
- 2、騰訊PHP面試題,PHP如何模擬POST提交登錄?求詳細代碼
- 3、php 模擬post 上傳
- 4、PHP 5.2用POST方式登錄,求解如何寫
- 5、php頁面向外網的asp頁面post表單數據實現模擬登陸,怎麼實現
- 6、怎麼用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面試題,PHP如何模擬POST提交登錄?求詳細代碼
大概流程是
先構建要傳輸的數據
再使用php的stocket模擬post請求
例子,比如我打開這個頁面所用到的數據就是(這裡用的是GET請求,改成POST就行了)
構造出以上的字符串,然後使用stocket發送出去即可
$fp = fsockopen(主機ip,端口號);
fputs($fp, 數據字符串);
while(!feof($fp)) {
//這裡是輸出請求所得到的回應數據
$result .= fgets($fp, 128);
}
更多請自行百度php模擬post請求
因為我以前在工程實例中做過,所以比較了解
純手打,望採納
話說,這個問題過了就能進騰訊?門檻太低了吧。。。
php 模擬post 上傳
你寫一個方法把,在php裡面可以使用curl庫來模擬這樣的表單 代碼如下:
//curl實現post請求
public function curl_post($url, $data = null){
//創建一個新cURL資源
$curl = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 跳過證書檢查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 從證書中檢查SSL加密算法是否存在
//設置URL和相應的選項
curl_setopt($curl, CURLOPT_URL, $url);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//執行curl,抓取URL並把它傳遞給瀏覽器
$output = curl_exec($curl);
//關閉cURL資源,並且釋放系統資源
curl_close($curl);
return $output;
}
PHP 5.2用POST方式登錄,求解如何寫
HTML中:(頭尾的head、body之類的我就省了)
form action=”xxx.php” method=”post”
用戶名:input type=”text” name=”name” /br/
密碼:input type=”password” name=”password” /br/
input type=”submit” value=”登錄” /
/form
PHP中:
?php
//HTML的form表單中,action指向此PHP文件,method=”post”時,利用$_POST即可獲取到提交的內容
if($_POST[‘name’]==’user’ $_POST[‘password’]==’123456′){
echo ‘Y’;
}else{
echo ‘N’;
}
//直接使用if判斷,如果用戶名為user,密碼為123456,則輸出Y,否則為N
?
php頁面向外網的asp頁面post表單數據實現模擬登陸,怎麼實現
這個實現方式太多了。可以用ajax跨域提交數據。可以用PHP的擴展類curl進行模樣表單提交。用JS跨域一直是一個問題,但可以實現,用jquery的時候你要注意這點,只有ajax()這個方法。用curl是不存在跨域問題的。但你要注意的是:curl模樣表單提交的時候,提交的字段一定要按字母集的順序(abcdef..)中文字符一定要用urlencode()函數進行編碼就可以了。要怎麼提交,下一個firebug查看一下就知道了
怎麼用php模擬post提交請求得到相應,能給個例子嗎
//這是我寫的封裝類,也就是模擬POST提交
//$durl也就是URL地址,比如
//$data 為POST數組
//模擬POST提交的用途,採集數據,模擬用戶登錄(為什麼現在登錄需要驗證碼,不是為了測你智商..
更不是考你的眼力,而防止你模擬登錄..用暴力破解法強行破解密碼)
function curl_file_post_contents($durl, $timeout = 5, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $durl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
curl_setopt($ch, CURLOPT_REFERER, _REFERER_);
curl_setopt($ch, CURLOPT_POST, 1); //設置為POST傳輸
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //添加post數據
$r = curl_exec($ch);
var_dump($ch);
if ($r === false) { //判斷錯誤
echo curl_error($ch);
}
$info = curl_getinfo($ch); //能夠在cURL執行後獲取這一請求的有關信息
curl_close($ch);
return $r;
}
原創文章,作者:SDRGG,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/129723.html