如何使用fsockopen从cookie.txt发送cookie和http请求?

时间:2017-02-02 01:55:18

标签: php cookies http-headers fsockopen

我正在搜索如何使用curl(发送http请求)而不等待响应完成,因为我只想触发许多操作而不期望结果。经过一段时间的搜索,我找到了一个解决方案Here,但我遇到了问题。
问题是我不知道如何传递我保存在.txt中的cookie,我想发送它使用此请求。

function curl_post_async($url, $params)
{
    foreach ($params as $key => &$val) {
        if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
    }

    $post_string = implode('&', $post_params);

    $parts=parse_url($url);

    $fp = fsockopen($parts['host'],
    isset($parts['port'])?$parts['port']:80,
    $errno, $errstr, 30);

    $out = "POST ".$parts['path']." HTTP/1.1\r\n";
    $out.= "Host: ".$parts['host']."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);
    fclose($fp);
}

如何使用上面的代码从.txt文件发送cookie?

1 个答案:

答案 0 :(得分:0)

经过研究,我解决了如何从cookies.txt发送cookie 当我搜索时,我找到了2种格式的饼干  1. Netscape

# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_.site.com TRUE    /   FALSE   1517538526  __cfduid    dc0d1a86523f4fa68c7c9e19b084aa1651486002526
#HttpOnly_www.site.com  FALSE   /   FALSE   1486006126  cisession   a914d92115f6a6d73124ac5de4c49e070b872b98

2。 Cookie默认格式(参考:Here

csrf_cookie_name=cac551cc13952929efd2507797fff687; ci_session=umt92quvucfu333p1co19b83i3jaglm9;

我尝试使用此行代码

发送cookie格式no.1
$out.= "Cookie: ".file_get_contents(getcwd().$cookie); //where getcwd().$cookie is location of the cookie.txt with format no.1

我尝试了但是失败了。但是如果cookie.txt的cookie值是2号格式,我发送了该cookie并成功发送并且可以读取。
结论发送cookie:

$out.= "Cookie: csrf_cookie_name=cac551cc13952929efd2507797fff687; ci_session=umt92quvucfu333p1co19b83i3jaglm9;";