当前位置:飞豹网络PHP → php模拟Cookie

PHP

php模拟Cookie

function getCookieUrl($host_url, $script_name, $method = 'GET', $data = '', $cookie_str = '')
{
    $sock = fsockopen($host_url, 80, $errno, $errstr, 30);
    if (!$sock) die("$errstr ($errno)\n");
    $method = strToUpper($method);
    $method = ($method == 'GET') ? 'GET' : 'POST';
    if (substr($script_name, 0, 1) != '/') {
        $script_name = '/' . $script_name;
    }

    fwrite($sock, $method . " " . $script_name . " HTTP/1.0\r\n");
    fwrite($sock, "Host: " . $host_url . "\r\n");
    if (!empty($cookie_str)) {
        fwrite($sock, "COOKIE: " . $cookie_str . "\r\n");
    }
    fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");
    if (!empty($data)) {
        fwrite($sock, "Content-length: " . strlen($data) . "\r\n");
    }
    fwrite($sock, "Accept: */*\r\n");
    fwrite($sock, "\r\n");
    if (!empty($data)) {
        fwrite($sock, "$data\r\n");
    }
    fwrite($sock, "\r\n");

    $body = "";
    while (!feof($sock)) {
        $body .= fgets($sock, 4096);
    }
    fclose($sock);
    return $body;

}

//以某个论坛的回贴举例:
$host_url = 'www.abc.com';
$message = '你要提交的内容';
$data = 'formhash=f887079a&subject=' . urlencode('文章标题!') . '&message=' . urlencode($message);
$tid = 131358;//贴子的ID

$body = getCookieUrl($host_url, '/post.php?action=reply&fid=38&tid=' . $tid . '&extra=page%3D1&replysubmit=yes', 'POST', $data, 'cdb_sid=vzlrbA;cdb_cookietime=2592000;cdb_auth=DwBQUABbAwUJCg9fUwQFVVICX1cKUgNWUApXXQVZV14xOlcFAw5Q');

echo $body;

//抓取数据,可以这样:
$body = getCookieUrl($host_url, '/test.php?id=33', 'GET', '', 'username=test;other_cookie:other');

0 个回复:

发表评论: