php curl cookie没有注册(php curl cookie not registering)

我一直在玩这个 curl facebook登录脚本一段时间只是试图掌握卷曲中的一些功能,但似乎我无法让cookie注册:

PHP脚本

function facebookLogin(){ $login_email = 'email'; $login_pass = 'pass'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.facebook.com/login.php'); curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); curl_setopt($ch, CURLOPT_COOKIEFILE, "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"); curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com"); $page = curl_exec($ch); echo $page; }

我有一个名为cookies.txt的文本文件,它与脚本位于同一目录中,但是在运行此脚本之后没有任何内容写入文件,因此没有创建cookie,这在尝试浏览其他网页时是个大问题。您必须保持登录的网站。

我哪里错了?

I've been playing with this curl facebook login script for a while just trying to get to grips with some of the features in curl, but it seems that I can not get the cookies to register:

php script

function facebookLogin(){ $login_email = 'email'; $login_pass = 'pass'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.facebook.com/login.php'); curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); curl_setopt($ch, CURLOPT_COOKIEFILE, "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"); curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com"); $page = curl_exec($ch); echo $page; }

I have a text file called cookies.txt which is in the same directory as the script, but after running this script nothing is written into the file and therefore no cookies are created, this is a big issue when trying to explore other web pages on the same website as you have to keep logging in.

Where am I going wrong?

最满意答案

好吧事实证明它已注册,即使cookies.txt文件为空,但你需要确保在尝试探索网站的其他部分时调用此文件,例如

function facebookGoToMessages(){ facebookLogin(); $ch = curl_init ("http://www.facebook.com/messages"); curl_setopt ($ch, CURLOPT_COOKIEFILE, "cookies.txt"); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 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"); curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com"); $page = curl_exec ($ch); echo $page; }

Ok it turns out it is registered even if the cookies.txt file is empty but you need to make sure you call this file when you try to explore other parts of the site e.g.

function facebookGoToMessages(){ facebookLogin(); $ch = curl_init ("http://www.facebook.com/messages"); curl_setopt ($ch, CURLOPT_COOKIEFILE, "cookies.txt"); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 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"); curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com"); $page = curl_exec ($ch); echo $page; }

更多推荐