使用CURL PHP登录时获取Facebook cookie

时间:2017-04-24 07:33:52

标签: php facebook curl cookies

我想获取Facebook Cookie然后将其保存到文本文件中。我可以成功登录但是我无法将cookie保存为txt格式的目录。

 $cookie = 'cookie.txt'
 function Get_Login($em,$pa,$cookie){
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, 'https://m.facebook.com/login.php');
 curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($em).'&pass='.urlencode($pa).'&login=Login');
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch,CURLOPT_HTTPHEADER,$ip);
 curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
 curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);              
 curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+');

  curl_setopt($ch, CURLOPT_REFERER,"https://www.facebook.com");

  $body = curl_exec($ch) or die(curl_error
  ($ch));
  }

使用此代码我可以成功登录。我想将cookies文件保存到我的目录中。

1 个答案:

答案 0 :(得分:0)

<?php 

      $body = curl_exec($ch) or die(curl_error($ch));
      $cookie = 'cookie.txt';
      // Open the file to get existing content
      $current = file_get_contents($cookie);
      // Append a new person to the file
      $current .= $body;
      // Write the contents back to the file
      file_put_contents($cookie, $current);
  ?>