如何從一個(gè)php文件向另一個(gè)地址post數(shù)據(jù),不用表單和隱藏的變量的
更新時(shí)間:2007年03月06日 00:00:00 作者:
可以使用以下函數(shù)來實(shí)現(xiàn):
<?php
function posttohost($url, $data) {
$url = parse_url($url);
if (!$url) return "couldn't parse url";
if (!isset($url['port'])) { $url['port'] = ""; }
if (!isset($url['query'])) { $url['query'] = ""; }
$encoded = "";
while (list($k,$v) = each($data)) {
$encoded .= ($encoded ? "&" : "");
$encoded .= rawurlencode($k)."=".rawurlencode($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)) {
$line = fgets($fp,1024);
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
elseif (!$inheader) {
$results .= $line;
}
}
fclose($fp);
return $results;
}
?>
--------------------------------------------------------------------------------------------------
也可以這樣
<?php
$URL="www.mysite.com/test.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://$URL");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Data1=blah&Data2=blah");
curl_exec ($ch);
curl_close ($ch);
?>
您可能感興趣的文章:
- PHP獲取POST數(shù)據(jù)的幾種方法匯總
- PHP下使用CURL方式POST數(shù)據(jù)至API接口的代碼
- 使用PHP接收POST數(shù)據(jù),解析json數(shù)據(jù)
- PHP的cURL庫功能簡介 抓取網(wǎng)頁、POST數(shù)據(jù)及其他
- 淺談PHP接收POST數(shù)據(jù)方式
- PHP函數(shù)分享之curl方式取得數(shù)據(jù)、模擬登陸、POST數(shù)據(jù)
- PHP基于CURL進(jìn)行POST數(shù)據(jù)上傳實(shí)例
- PHP中使用file_get_contents post數(shù)據(jù)代碼例子
- PHP中使用socket方式GET、POST數(shù)據(jù)實(shí)例
- php使用socket post數(shù)據(jù)到其它web服務(wù)器的方法
- php獲取POST數(shù)據(jù)的三種方法實(shí)例詳解
相關(guān)文章
PHP中header和session_start前不能有輸出原因分析
在http傳輸文本中,規(guī)定必須 header和content順序必須是:header在前content在后,并且header的格式必須滿足“keyword: value\n”這種格式,大家知道這是為什么嗎?接下來為您詳細(xì)解答2013-01-01PHP實(shí)現(xiàn)十進(jìn)制數(shù)字與二十六進(jìn)制字母串相互轉(zhuǎn)換操作示例
這篇文章主要介紹了PHP實(shí)現(xiàn)十進(jìn)制數(shù)字與二十六進(jìn)制字母串相互轉(zhuǎn)換操作,涉及php字符串遍歷、轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2018-08-08php實(shí)現(xiàn)背景圖上添加圓形logo圖標(biāo)的方法
這篇文章主要介紹了php實(shí)現(xiàn)背景圖上添加圓形logo圖標(biāo)的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了php背景圖添加logo圖標(biāo)的操作步驟與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-11-11PHP實(shí)現(xiàn)通用alert函數(shù)的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)通用alert函數(shù)的方法,實(shí)例分析了php自定義alert函數(shù)實(shí)現(xiàn)提示信息的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03