0

I have read and try all possible uses of php cURL but with no luck.

Is possible to login from my-site.com using cURL to remote-site.com and next open remote-site.com and browse it as logged user?

Web browser does not seems to save session cookies after cURL request.

last try:

$cfile = tempnam ('C:/tmp', 'NCOOKIE');

$data = array (
    'username' => 'username',
    'password' => 'password'
);

$c = curl_init();
curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLOPT_NOBODY, false);
curl_setopt($c, CURLOPT_URL, 'http://remote-site.com/login.php');
curl_setopt($c, CURLOPT_COOKIESESSION, true);
curl_setopt($c, CURLOPT_COOKIEJAR, $cfile);
curl_setopt($c, CURLOPT_COOKIEFILE, $cfile);
curl_setopt($c, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, http_build_query($data));

session_write_close();
$g = curl_exec($c);
session_start();

$hs = curl_getinfo($c, CURLINFO_HEADER_SIZE);   // header size
$h = substr($g, 0, $hs);    // header
$g = substr($g, $hs);   // body

curl_close($c);
AkisC
  • 817
  • 2
  • 12
  • 27
  • 1
    You mean "log into the site via cURL" and *then* browse the site via a standard web-browser (like Chrome)? – newfurniturey Jan 08 '15 at 13:58
  • Yes I meean that ... sorry for my bad English – AkisC Jan 08 '15 at 13:59
  • Very good question! I've been wondering about this too lately, it was for a programming challenge on wechall –  Jan 08 '15 at 14:01
  • Yeah, no... that's not going to work unless you can figure out how to store cookies *for that browser* (which is not trivial). What's the purpose of doing it this way instead of just logging into the site with the browser itself? – newfurniturey Jan 08 '15 at 14:01
  • The *opposite* is much easier to do though (logging into the website with a browser and then using the cookies in cURL)... if that matters? For example: http://stackoverflow.com/questions/21919156/how-do-i-copy-cookies-from-chrome – newfurniturey Jan 08 '15 at 14:03
  • @newfurniturey many purposes. For example I have on my-site.com database with users linked with remote-site.com database users. – AkisC Jan 08 '15 at 14:06

0 Answers0