網絡技術日誌
關於 PHP, Linux, Open Source 及個人生活記載的網誌。
-
不使用 form 之下傳送 POST 變數
Posted on December 11th, 2007 No comments可以打開 HTTP socket 連線及傳送 HTTP POST 指令,以下是範例:
PHP:-
<?php
-
// Generate the request header
-
$ReqHeader =
-
"POST $URI HTTP/1.1n".
-
"Host: $Hostn".
-
"Content-Type: application/x-www-form-urlencodedn".
-
"Content-Length: $ContentLengthnn".
-
"$ReqBodyn";
-
-
// Open the connection to the host
-
if (!$socket){
-
-
$Result["errno"] = $errno;
-
$Result["errstr"] = $errstr;
-
return $Result;
-
}
-
$idx = 0;
-
}
-
//-------------------------------------------
-
?>
或者可以使用 PHP 的 cURL extension。當你安裝了 cURL 及重新編譯 PHP 支援 cURL後,便可以用以下這個較簡單的方法:
PHP:-
<?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);
-
?>
Leave a reply
-
