How to use PHP cURL login without COOKIEFILE??

Discussion in 'PHP & Perl' started by trendslogger, Oct 23, 2014.

  1. trendslogger

    trendslogger Newbie

    Joined:
    Oct 23, 2014
    Messages:
    3
    Likes Received:
    0
    Location:
    Indonesia
    Hi everyone, I have a question about PHP. I 'm trying to make a script which login to twitter using PHP cURL, I'm using COOKIEFILE here and it works fine. Please my code below, read the comments too, all the explanation are there.


    PHP:
    <?php
    /* php curl function to enable cookie without cookiejar (I got it from github) */function fetch($url$cookies null$returnCookie false) {    $ch curl_init();    curl_setopt($chCURLOPT_URL$url);    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);    if($cookies){        curl_setopt($chCURLOPT_COOKIEimplode(';',$cookies));    }    curl_setopt($chCURLOPT_HEADER1);    $result curl_exec($ch);    list($header$body) = explode("\r\n\r\n"$result2);    $end strpos($header'Content-Type');    $start strpos($header'Set-Cookie');    $parts explode('Set-Cookie:'substr($header$start$end $start));    $cookies = array();    foreach ($parts as $co) {        $cd explode(';'$co);        if (!empty($cd[0]))            $cookies[] = $cd[0];    }    curl_close($ch);    if ($returnCookie){        return $cookies;    }    return json_decode($body);}
    /** * * my code starts here * */
    /* request new session on twitter login page */
    $request1 curl_init();curl_setopt_array($request1, Array(    CURLOPT_URL => 'hxx ps: / /mobile . twitter . c*m /session /new',    CURLOPT_FOLLOWLOCATION => false,    CURLOPT_SSL_VERIFYPEER => false,    CURLOPT_RETURNTRANSFER => true,    CURLOPT_COOKIEJAR => 'cookies.txt',    CURLOPT_TIMEOUT => 13,    CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36",    CURLOPT_HEADER => true));$response1 curl_exec($request1);curl_close($request1);
    /* get twitter authenticity_token */preg_match("/authenticity_token\" type=\"hidden\" value=\"([^\"]+)/i"$response1$token);$mytoken $token[1];
    /* data to be posted */$user 'USERNAME';$pass 'PASSWORD';$postdata "authenticity_token=" rawurlencode($mytoken) . "&username=" $user "&password=" $pass;
    /* next request which needs cookiefile */$request2  curl_init();curl_setopt_array($request2, Array(    CURLOPT_URL => 'hxx ps: / /mobile . twitter . c*m /session',    CURLOPT_SSL_VERIFYPEER => false,    CURLOPT_RETURNTRANSFER => true,    CURLOPT_FOLLOWLOCATION => false,    CURLOPT_COOKIEJAR => 'cookies.txt',    CURLOPT_COOKIEFILE => 'cookies.txt',    CURLOPT_TIMEOUT => 13,    CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36",    CURLOPT_HEADER => true,    CURLOPT_REFERER => 'hxx ps: / /mobile . twitter . c*m /session /new',    CURLOPT_CUSTOMREQUEST => "POST",    CURLOPT_POST => true,    CURLOPT_POSTFIELDS => $postdata));$response2 curl_exec($request2);curl_close($request2);
    /* save response to txt file, for learning purpose */$check fopen('response.txt''w+');fwrite($check$response2);fclose($check);
    The code above works fine, but I've to login without cookiejar /cookiefile since the server I want to use doesn't support file-system, which means they don't allow me to write any file using PHP, including cookiejar /cookiefile.


    MY QUESTION : how to use fetch(); function at the top so that I can login to twitter WITHOUT COOKIEJAR / COOKIEFILE ???


    I hope someone can help me solve the code, thanks.
     
  2. trendslogger

    trendslogger Newbie

    Joined:
    Oct 23, 2014
    Messages:
    3
    Likes Received:
    0
    Location:
    Indonesia
    the code above doesn't seem like it's readable, please see the code here : pastebin.c*m/k9mihixd
     
  3. member8200

    member8200 Regular Member

    Joined:
    Aug 9, 2014
    Messages:
    475
    Likes Received:
    33
    i can't get the code. and the code from the post is hard to read. :)

    any other options?