搜索
首页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脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。