【问题标题】:How to send cookie alongside http request from cookie.txt using fsockopen?如何使用 fsockopen 将 cookie 与来自 cookie.txt 的 http 请求一起发送?
【发布时间】:2017-02-02 01:55:18
【问题描述】:

我正在搜索如何使用 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?

【问题讨论】:

    标签: php cookies http-headers fsockopen


    【解决方案1】:

    经过研究,我解决了如何从 cookies.txt 发送 cookie
    当我搜索时,我发现了 2 种 cookie 格式
    1.网景

    # 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 格式 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;";
    

    【讨论】:

      猜你喜欢
      • 2018-04-15
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多