當前位置:首頁 » 編程語言 » php模擬post

php模擬post

發布時間: 2022-08-05 11:09:16

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介面開發怎麼模擬post請求

介面無非就是接受數據然後處理返回結果,客戶端接收結果數據處理

/*apixxxxx.php*/
$data=$_POST;

數據處理.....

returnjson_encode($redata);

/*客戶端 js*/

$.post('xxxxx.php',data,fucnction(datas){
處理結果
});

類似上面的處理流程

⑶ 急需php模擬post的詳細代碼,有詳細的注釋了,有例子最好了

其實這段代碼並不難理解,本人表達能力不佳,見諒
<?php

$URL = ''; //需要提交到的頁面
//下面這段是要提交的數據
$post_data['SysID'] = $_POST['SysID'];
$post_data['FuncID'] = $_POST['FuncID'];
$post_data['LoginID'] = $_POST['LoginID'];
$post_data['LoginKey'] = $_POST['LoginKey'];
$referrer="";
$URL_Info=parse_url($URL);
if($referrer=="")
{
$referrer=$_SERVER["SCRIPT_URI"];
}
foreach ($post_data as $key=>$value)
{
$values[]="$key=".urlencode($value);
}

$data_string=implode("&",$values);//提交的數據格式是 a=1&b=2
// Find out which port is needed - if not given use standard (=80)
if (!isset($URL_Info["port"])) {
$URL_Info["port"]=80;
// building POST-request:
//一般做網站用form提交數據後,之後的操作就不用我們不管了,
//這里就是在模擬POST操作的HTTP頭,具體的可以去查HTTP協議的資料,並不復雜
$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
$request.="Host: ".$URL_Info["host"]."\n";
$request.="Referer: $referrer\n";
$request.="Content-type: application/x-www-form-urlencoded\n";
$request.="Content-length: ".strlen($data_string)."\n";
$request.="Connection: close\n";
$request.="\n";
$request.=$data_string."\n";
//exit;
}
//fsockopen的用法就這樣子了,不做多說明
$fp = fsockopen($URL_Info["host"], $URL_Info["port"]);
fputs($fp, $request);//把HTTP頭發送出去
while(!feof($fp)) {
//$result 是提交後返回的數據
$result .= fgets($fp, 1024);
}
fclose($fp);
?>

⑷ php 模擬登錄,post到多個地址怎麼做

$url = ''; //POST地址
$password = ''; //密碼
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'username='.$_COOKIE['username'].'&password='.$password);
/**
* 如果$url是https則需要取消下面兩行注釋
* curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
* curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
*/
curl_exec($curl);
curl_close($curl);

⑸ 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模擬HTTP的POST請求時遇到的問題求助

類中 靜態方法和靜態屬性的引用方法例如class Test{ public static $test = 1; public static function test(){ }}可以不用實例化對象直接使用 Test::$test 來取得$test屬性的值靜態方法調用也同理Test::test(); 直接調用靜態方法test

如果我解答樓主覺得不清楚可以自己去後盾人學習,對你學習PHP應該會有幫助,

⑺ php 模擬post ,如何不被伺服器封ip

既然論壇都封你ip了,說明用單個ip肯定不行,使用代理是必須的。
解決的思路是:
1. 准備多個代理;
2. 控制post速度,單位時間內不要post太頻繁;
3. 選擇一個代理post數據,達到一定時間(例如半小時)或post達到一定數量(例如100)時,切換代理;
4. 用新代理post數據,循環進行3-4步驟;

猜測代碼中可以改進的有:
1. 構造多種主流瀏覽器的頭信息,切換代理時使用不同的頭信息;
2. 盡量在post過程中加上伺服器端返回的cookie信息;

這些改動可以使你的模擬過程更接近真實的瀏覽器訪問數據。

⑻ php怎麼模擬GET與POST向微信介面提交及獲取數據的方法

用curl

GET方法:

//初始化
$ch=curl_init();
//設置選項,包括URL
curl_setopt($ch,CURLOPT_URL,"http://www..com");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
//執行並獲取HTML文檔內容
$output=curl_exec($ch);
//釋放curl句柄
curl_close($ch);
//列印獲得的數據
print_r($output);

POST方法:

$url="http://localhost/web_services.php";
$post_data=array("username"=>"bob","key"=>"12345");
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//post數據
curl_setopt($ch,CURLOPT_POST,1);
//post的變數
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
$output=curl_exec($ch);
curl_close($ch);
//列印獲得的數據
print_r($output);

⑼ 使用php curl 模擬post請求,自動附加了data參數

$post_data_string=http_build_query($post_data,'&');

$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$get_session_url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$xmloutput=curl_exec($ch);

一般這樣寫 你自己對比下

⑽ PHP里模擬Post提交是什麼意思

php
表單提交常見的就是post和get
模擬提交就是通過其他技術達到post或get的效果
php
常見的模擬就是curl方式了
作用比如說刷票
每次提交它可以模擬ip
逃過ip限制
圖片上傳
可以post提交
不用模擬

熱點內容
美國雲伺服器主機 發布:2024-04-19 22:28:54 瀏覽:139
抗生素資料庫 發布:2024-04-19 22:13:03 瀏覽:495
晚晚教編程 發布:2024-04-19 21:56:23 瀏覽:712
安卓換蘋果語音留言怎麼看 發布:2024-04-19 21:56:21 瀏覽:627
解壓神經 發布:2024-04-19 21:47:03 瀏覽:894
c語言字元轉義字元 發布:2024-04-19 21:43:51 瀏覽:727
mysql存儲過程語法 發布:2024-04-19 21:00:04 瀏覽:245
修復損壞的壓縮文件 發布:2024-04-19 20:53:32 瀏覽:423
編程發型 發布:2024-04-19 20:53:28 瀏覽:500
去除空格sql 發布:2024-04-19 20:43:30 瀏覽:785