利用php的socket編程來直接給接口發送數據來模擬post的操作。
<?
/***********************************************************
Name: POST 測試程序 Vesion: 1.0 Date: 2004-08-05 ************************************************************/
/ $flag = 0;
//要post的數據
$argv = array(
'var1'=>'abc',
'var2'=>'你好嗎');
//構造要post的字符串
foreach ($argv as $key=>$value) {
if ($flag!=0) {
$params .= "&";
$flag = 1;
}
$params.= $key."="; $params.= urlencode($value);
$flag = 1;
}
$length = strlen($params);
//創建socket連接
$fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
//構造post請求的頭
$header = "POST /mobile/try.php HTTP/1.1rn";
$header .= "Host:127.0.0.1rn";
$header .= "Referer:/mobile/sendpost.phprn";
$header .= "Content-Type: application/x-www-form-urlencodedrn";
$header .= "Content-Length: ".$length."rn";
$header .= "Connection: Closernrn";
//添加post的字符串
$header .= $params."rn";
//發送post的數據
fputs($fp,$header);
$inheader = 1;
while (!feof($fp)) {
$line = fgets($fp,1024); //去除請求包的頭只顯示頁面的返回數據
if ($inheader && ($line == "n" || $line == "rn")) {
$inheader = 0;
}
if ($inheader == 0) {
echo $line;
}
}
fclose($fp);
?>