curl进行身份验证但不返回任何网页

时间:2012-10-18 20:41:29

标签: php curl

此脚本AUTH成功,但它不会正确返回网页,服务器返回。

HTTP/1.1 100 Continue HTTP/1.1 303 See Other Date: Thu, 18 Oct 2012
20:36:45 GMT Server:    Apache/2.2.14 (Ubuntu) X-Powered-By:
PHP/5.2.6-3ubuntu4.6 Set-Cookie:
MoodleSessionmain=9b04143933c4ae2564e5be247fc8463d; path=/ Expires:
Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache,
must-revalidate, post-check=0, pre-check=0 Pragma: no-cache
Set-Cookie: MoodleSessionTestmain=Qf2kfwifF3; path=/ Set-Cookie:
MOODLEID_main=deleted; expires=Wed, 19-Oct-2011 20:36:44 GMT; path=/
Set-Cookie: MOODLEID_main=%25E2%25C2%2519C%25BDg%25B1%2502%25E5%255D;
expires=Mon, 17-Dec-2012 20:36:45 GMT; path=/ Location:
http://main.ingeniat.com/my/index.php Vary: Accept-Encoding
Content-Length: 202 Connection: close Content-Type: text/html
Set-Cookie: SERVERID=guia5; path=/ HTTP/1.1 200 OK Date: Thu, 18 Oct
2012 20:36:45 GMT Server: Apache/2.2.14 (Ubuntu) X-Powered-By:
PHP/5.2.6-3ubuntu4.6 Expires: Cache-Control: private, pre-check=0,
post-check=0, max-age=0 Pragma: no-cache Content-Script-Type:
text/javascript Content-Style-Type: text/css Content-Language: es
Accept-Ranges: none Vary: Accept-Encoding Connection: close
Transfer-Encoding: chunked Content-Type: text/html; charset=utf-8

这是代码

$post_fields = array();
$post_fields['username']=$usr;
$post_fields['password']=$pass;

$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 1);

curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields );


curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");

echo curl_exec($ch);
curl_close($ch);

我做错了什么?

2 个答案:

答案 0 :(得分:1)

CURLOPT_HEADER没有提及排除身体的任何事情。 http://php.net/manual/en/function.curl-setopt.php

但也许您的网页重定向次数超过10次?也许添加

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

然后发布新标题。

答案 1 :(得分:0)

当您使用数组设置CURLOPT_POSTFIELDS时,CURL会将请求类型设置为multipart / form-data,而服务器需要application / x-www-form-urlencoded,因此您需要在帮助下自行编码字符串urlencode