获得需要session和登录的页面内容的PHP函数

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;

}

 

你可能感兴趣的:(PHP)