Skip to content

Instantly share code, notes, and snippets.

@eristoddle
Created January 31, 2014 19:12
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save eristoddle/8740954 to your computer and use it in GitHub Desktop.
Save eristoddle/8740954 to your computer and use it in GitHub Desktop.
PHP curl with cookies and no text files needed
<?php
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);
}
@ciptard
Copy link

ciptard commented Oct 21, 2014

Hi, I want to combine your function with my code, the server that I want to use doesn't support file-system, so I've to get rid of cookiefile. the code below works fine, response2.txt show me that I've logged in to twitter.

do you mind helping me solve the code?

// request twitter login page
$request1 = curl_init();
curl_setopt_array($request1, Array(
    CURLOPT_URL => 'https://mobile.twitter.com/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,
    CURLINFO_HEADER_OUT > true
));
$response1 = curl_exec($request1);
curl_close($request1);

// save response to txt file, for learning purpose
$check_1 = fopen('response1.txt', 'w+');
fwrite($check_1, $response1);
fclose($check_1);

// get twitter authenticity_token
preg_match("/authenticity_token\" type=\"hidden\" value=\"([^\"]+)/i", $response1, $token);
$mytoken = $token[1];


$user = 'USERNAME';
$pass = 'PASSWORD';
$postdata = "authenticity_token=" . rawurlencode($mytoken) . "&username=" . $user . "&password=" . $pass;

$request2  = curl_init();
curl_setopt_array($request2, Array(
    CURLOPT_URL => 'https://mobile.twitter.com/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,
    CURLINFO_HEADER_OUT > true,
    CURLOPT_REFERER => 'https://mobile.twitter.com/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_2 = fopen('response2.txt', 'w+');
fwrite($check_2, $response2);
fclose($check_2);

I hope you can spend some time to help me, thanks..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment