Using CURL and PHP to get facebook cookie on login

I am trying to get my_cookie.txt with a facebook cookie so that I can reuse it with CURL for other facebook pages.

Here is my PHP code ... When I try it ... I see a facebook login page, but it gives me a message that my cookies in my browser should be enabled ... BUT they are included.

$email = 'my email'; $password = 'mypassword'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php'); curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($email).'&pass='.urlencode($password)); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt"); curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"); $page = curl_exec($ch); echo $page; 

Here is the error I see

enter image description here

Any idea what I'm doing wrong?

+6
source share
4 answers

First of all, it is against TOS facebook (it is better to use api).

Logging in is simply not to get to the login page. First, you must first browse the homepage (using twisting in this case) and save the cookie to the bank. Then, during login, use a cookie requesting login. You may need to find a token or something similar on the home page (if they use one).

So, the steps for any site you want to make a login request to are:

1) View your homepage using curl. Store cookies using a jar. And do not forget to close the curl.

2) If there is any token or hidden parameter on the page, analyze this from html.

3) Initialize a new curl, prepare the post parameter with the user / pass + hidden parameters. Remember to add a jar of cookies.

+4
source

try adding these lines:

 curl_setopt( $ch, CURLOPT_COOKIESESSION, true ); curl_setopt( $ch, CURLOPT_COOKIEJAR, uniquefilename ); curl_setopt( $ch, CURLOPT_COOKIEFILE, uniquefilename ); 

From: PHP Curl And Cookies

+1
source

Hi, did you try to set the resolution of the my_cookies.txt file to 644? In addition, you will need all three lines above.

 $cookie_file = "my_cookies.txt"; curl_setopt($ch, CURLOPT_COOKIESESSION, true); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); 
0
source

Some cookies called datr must be set using javascript or login does not work. Therefore, I received this cookie from my browser and added this line to the cookie and successfully logged in!

0
source

Source: https://habr.com/ru/post/980842/


All Articles