搜尋
首頁php教程PHP源码php中post模拟提交数据实例四种方法

在php中有很多种方法可以实现模拟提交的,下面我整理了四种post提交数据的例子,希望对各位同学会带来帮助。

<script>ec(2);</script>
 代码如下 复制代码

 
//以程序登陆一个论坛登录为例
function bbslogin($user_login, $password, $host, $port = "80") {
    //需要提交的post数据
    $argv = array('cookie' => array('user_login' => $user_login, 'password' => $password, '_wp_http_referer' => '/bbpress/', 're' => '', 'remember' => true));
    foreach ($argv['cookie'] as $key => $value) {
        $params[] = $key . '=' . $value;
    }
    $params = implode('&', $params);
    $header = "POST /bbpress/bb-login.php HTTP/1.1rn";
    $header .= "Host:$host:$portrn";
    $header .= "Content-Type: application/x-www-form-urlencodedrn";
    $header .= "Content-Length: " . strlen($params) . "rn";
    $header .= "Connection: Closernrn";
    $header .= $params;
    $fp = fsockopen($host, $port);
    fputs($fp, $header);
    while (!feof($fp)) {
        $str = fgets($fp);
        //以下是自己的逻辑代码,这里主要是模拟cookie,可用来同步登陆
        if (!(strpos($str, "Set-Cookie:") === false)) {
            $tmparray = explode(" ", $str);
            $cookiearray = explode("=", $tmparray[1]);
            $cookiepaths = explode("=", $tmparray[6]);
            $cookiename = urldecode($cookiearray[0]);
            $cookievalue = urldecode(substr($cookiearray[1], 0, strlen($cookiearray[1]) - 1));
            $cookietime = time() + 3600 * 24 * 7;
            $cookiepath = urldecode(substr($cookiepaths[1], 0, strlen($cookiepaths[1]) - 1));
            setcookie($cookiename, $cookievalue, $cookietime, $cookiepath);
        }
    }
    fclose($fp);
}
?>
 
 
// PHP POST数据的三种方法
// php有三种方法可以post数据,分别为Curl、socket、file_get_contents:
 
 
/**
 * Socket版本
 * 使用方法:
 * $post_string = "app=socket&version=beta";
 * request_by_socket('facebook.cn','/restServer.php',$post_string);
 */
function request_by_socket($remote_server, $remote_path, $post_string, $port = 80, $timeout = 30)
{
    $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
    if (!$socket) die("$errstr($errno)");
 
    fwrite($socket, "POST $remote_path HTTP/1.0rn");
    fwrite($socket, "User-Agent: Socket Examplern");
    fwrite($socket, "HOST: $remote_serverrn");
    fwrite($socket, "Content-type: application/x-www-form-urlencodedrn");
    fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . 'rn');
    fwrite($socket, "Accept:*/*rn");
    fwrite($socket, "rn");
    fwrite($socket, "mypost=$post_stringrn");
    fwrite($socket, "rn");
    $header = "";
    while ($str = trim(fgets($socket, 4096))) {
        $header .= $str;
    }
    $data = "";
    while (!feof($socket)) {
        $data .= fgets($socket, 4096);
    }
    return $data;
}
 
 
 
/**
 * Curl版本
 * 使用方法:
 * $post_string = "app=request&version=beta";
 * request_by_curl('http://facebook.cn/restServer.php',$post_string);
 */
function request_by_curl($remote_server, $post_string)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $remote_server);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Jimmy's CURL Example beta");
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
 
 
/**
 * 其它版本
 * 使用方法:
 * $post_string = "app=request&version=beta";
 * request_by_other('http://facebook.cn/restServer.php',$post_string);
 */
function request_by_other($remote_server, $post_string)
{
    $context = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type: application/x-www-form-urlencoded' .
                        'rn'.'User-Agent : Jimmy's POST Example beta' .
                        'rn'.'Content-length:' . strlen($post_string) + 8,
            'content' => 'mypost=' . $post_string)
        );
    $stream_context = stream_context_create($context);
    $data = file_get_contents($remote_server, false, $stream_context);
    return $data;
}
 
?>
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具