PHP Curl登录,重定向无法正常工作

时间:2012-10-02 11:10:06

标签: php cookies curl

我有以下PHP脚本,它应该获取页面内容,即登录表单后面。

它在我的localhost服务器上运行正常,但它不能在主机(GoFreeServe)上运行。我得到一个空洞的回应。 ($ postResult为空)。

脚本:

    $login_url = 'http://senioren.voetbal.nl/clubs_comp/mijn-teams/competitie/173299/OWIOS-9-zaterdag?destination=clubs_comp%2Fmijn-teams%2Fcompetitie%2F173299%2FOWIOS-9-zaterdag';
$data = file_get_contents("http://voetbal.nl/login/menu/data");
        $parts = explode('name=\"form_build_id\" id=\"',$data,2);
$post_data = 'name=****&pass=****&op=Inloggen&form_build_id='.substr($parts[1],0,37).'&form_id=login_block_login_form';
$ch = curl_init();
$agent = $_SERVER["HTTP_USER_AGENT"];
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $login_url);
curl_setopt($ch, CURLOPT_URL, $login_url );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

$postResult = curl_exec($ch);

curl_getinfo Localhost

[url] => http://senioren.voetbal.nl/clubs_comp/mijn-teams/competitie/173299/OWIOS-9-zaterdag
    [content_type] => text/html; charset=utf-8
    [http_code] => 200
    [header_size] => 557
    [request_size] => 503
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 2.668
    [namelookup_time] => 0
    [connect_time] => 0.016
    [pretransfer_time] => 0.016
    [size_upload] => 57
    [size_download] => 173015
    [speed_download] => 64848
    [speed_upload] => 21
    [download_content_length] => 173015
    [upload_content_length] => 57
    [starttransfer_time] => 1.014
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

    [redirect_url] => 

curl_getinfo主持人

Array
(
    [url] => http://senioren.voetbal.nl/clubs_comp/mijn-teams/competitie/173299/OWIOS-9-zaterdag
    [content_type] => text/html; charset=utf-8
    [http_code] => 302
    [header_size] => 1125
    [request_size] => 361
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 1.001361
    [namelookup_time] => 0.000638
    [connect_time] => 0.094666
    [pretransfer_time] => 0.094712
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 1.001199
    [redirect_time] => 0
)

主机不在safe_mode中并且已安装CURL。还将cookie.txt修改为777。 可以在此处找到主机的PHPInfo:http://phpinfo.byethost11.com/index.php

你知道我为什么在我的Localhost上获得一个200的http_code,在主机上获得一个302的http_code?

谢谢!

编辑: 打开错误报告(!@!#!$)后,页面返回以下错误:

Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /test2.php on line 39

3 个答案:

答案 0 :(得分:1)

首先检查是否设置了open_basedir,我解决了这个问题。 如果是这种情况:脚本首先执行curl_exec()来设置cookie,然后再次执行它以获取隐藏页面。像op和form_build_id这样的参数根本没有使用:P

代码:

if (ini_get('open_basedir') == '') {
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

    $postResult = curl_exec($ch);
    curl_close($ch);
} else {
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_exec($ch);
    $postResult = curl_exec($ch);
}

答案 1 :(得分:0)

尝试添加FOLLOWLOCATION选项,使其遵循它所获得的重定向:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

答案 2 :(得分:0)

form_build_idCURLOPT_REFERER丢失您可能会发送类似

的内容
 name=user&pass=pass&op=Inloggen&form_build_id=form-fc3c69bda9d4a9a4a53e70f836ece383&form_id=login_block_login_form