我正在使用PHP实现一个项目,因为我想自动登录页面.代码如下.
$ch = curl_init(); $postdata="Email=$username&Passwd=$password&continue=https://www.mail.google.com"; curl_setopt ($ch, CURLOPT_URL,"https://www.google.com"); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); curl_setopt ($ch, CURLOPT_TIMEOUT, 60); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_COOKIEJAR, $gacookie); curl_setopt ($ch, CURLOPT_COOKIEFILE, $gacookie); curl_setopt ($ch, CURLOPT_REFERER, 'https://www.google.com'); curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); curl_setopt ($ch, CURLOPT_POST, 1); $AskApache_result = curl_exec ($ch); curl_close($ch); echo $AskApache_result; unlink($gacookie); ?>
但它不会起作用.有任何想法吗?
首先:"它不起作用"并没有给我们太多的信息和背景来帮助你解决问题.
要回答您的问题,请尝试提供详细信息,例如:
哪个具体部分不起作用?
你收到任何错误信息吗?
您运行的代码是什么操作系统?
它还有助于使用更具描述性的问题标题."PHP和CURL"并没有给我们提供太多相关信息,而"如何在执行CURL请求时解决错误x123"会更有帮助.
无论如何.查看代码,至少有一个错误:
curl_setopt ($ch, CURLOPT_REFERER, 'https://www.google.com");
应该:
curl_setopt ($ch, CURLOPT_REFERER, "https://www.google.com");
澄清:第三个参数以单引号开头,以双引号结束.