trendslogger
Newbie
- Oct 23, 2014
- 3
- 0
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.
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.
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($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if($cookies){ curl_setopt($ch, CURLOPT_COOKIE, implode(';',$cookies)); } curl_setopt($ch, CURLOPT_HEADER, 1); $result = curl_exec($ch); list($header, $body) = explode("\r\n\r\n", $result, 2); $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.