PHP发送带有Cookie的POST请求

时间:2018-10-27 13:04:15

标签: php

我正在尝试通过Cookie向网站发送POST请求。就像PHP使它变得复杂一样简单,我不知道该怎么做。

下面的内容并没有发送我想要的cookie。

    $ch = curl_init("https://auth.roblox.com/v2/logout");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array()));
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_COOKIE, ".ROBLOSECURITY=" . $cookie);
    $resp = curl_exec($ch);
    print_r ($resp);

我正在尝试将此python代码转换为PHP; (有效)

    jar = r.cookies.RequestsCookieJar()
    jar.set('.ROBLOSECURITY', cookie, domain='.roblox.com', path='/')
    resp = r.post(url="https://auth.roblox.com/v2/logout", cookies=jar)

输出标题:

    HTTP/1.1 401 Unauthorized
    Cache-Control: no-cache
    Pragma: no-cache
    Content-Length: 83
    Content-Type: application/json
    Expires: -1
    X-Frame-Options: SAMEORIGIN
    Roblox-Machine-Id: RP-WEB350
    P3P: CP="CAO DSP COR CURa ADMa DEVa OUR IND PHY ONL UNI COM NAV INT DEM 
    PRE"
    Date: Sat, 27 Oct 2018 13:24:09 GMT

输出主体:

    {"errors":[{"code":0,"message":"Authorization has been denied for this 
    request."}]}

0 个答案:

没有答案