2017-04-24 59 views
0

我想獲取Facebook Cookie,然後將其保存到文本文件中。我可以登錄成功但我不能保存cookies我的目錄爲txt格式。使用CURL登錄時獲取Facebook Cookie PHP

$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)); 
    } 

使用此代碼,我可以成功登錄。我想將cookie文件保存到我的目錄中。

+0

您不應該使用任何此類自動操作登錄到Facebook。如果你想與Facebook交互 - 使用他們的API。 – CBroe

回答

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); 
    ?> 
+0

我試試這個$ current = file_get_contents($ cookie);顯示錯誤找不到文件或目錄 – bonuf

+0

首先,您必須在目錄中創建一個文件:<?php $ file ='cookie.txt'; $ handle = fopen($ file,'w')or die('Can not open file:'。$ file); //隱式創建文件 ?>' –