自动化MaxMind登录和数据库下载

时间:2016-08-30 16:57:40

标签: php maxmind

我尝试使用PHP CURL登录MaxMind帐户并下载特定文件,我甚至无法使登录部分工作,以下返回" false" :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.maxmind.com/en/account');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

var_dump($result);

2 个答案:

答案 0 :(得分:1)

自动下载文件的一种更简单的方法是转到https://www.maxmind.com/en/download_files并复制所需数据库格式的链接URL。如果您希望始终下载最新的数据库,请从URL中删除日期参数。如果您使用shell脚本中的wget或curl,请务必将URL放在引号内。

如果您要下载dat或mmdb格式,还有一个程序可以处理自动更新,http://dev.maxmind.com/geoip/geoipupdate/

答案 1 :(得分:0)

根据您上面的陈述,我将以一种重新散列的格式添加我的评论以及一些额外的代码 - 但是您似乎已经找到了一个解决方案,并且已经接受了答案但是没关系。

通常,对SSL端点的curl请求需要配置更多选项,特别是在许多情况下,使用有效的cacert.pem文件和显式选项来验证SSL主机。在原始代码中选择的auth方法更适合登录受.htaccess文件保护的受限区域,而不是标准的基于Web的登录表单,其中post请求中使用的字段应遵循表单中给出的名称元素。通常,在尝试模拟Web登录时,我将复制相关表单,然后找到所有input元素,并在我提供给curl请求的POST数据中使用它们。

/* A simple curl function */
function mmcurl( $url=NULL, $data=array(), $options=NULL ){

    /* Download cacert.pem and change path here to suit */
    $cacert='c:/wwwroot/cacert.pem';
    $cookiejar=tempnam( sys_get_temp_dir(), '_cookiejar_' );                    


    $curl=curl_init();

    if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
        curl_setopt( $curl, CURLOPT_CAINFO, realpath( $cacert ) );
    }

    curl_setopt( $curl, CURLOPT_URL,trim( $url ) );
    curl_setopt( $curl, CURLOPT_AUTOREFERER, TRUE );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE );
    curl_setopt( $curl, CURLOPT_FRESH_CONNECT, TRUE );
    curl_setopt( $curl, CURLOPT_HEADER, FALSE );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE );
    curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0' );
    curl_setopt( $curl, CURLOPT_ENCODING, '' );

    if( $options->cookie ){
        curl_setopt( $curl, CURLOPT_COOKIEFILE, $cookiejar );
        curl_setopt( $curl, CURLOPT_COOKIEJAR, $cookiejar );
        curl_setopt( $curl, CURLOPT_COOKIE, $cookiejar );
    }
    if( $options->post ){
        curl_setopt( $curl, CURLOPT_POST, true );
        curl_setopt( $curl, CURLOPT_POSTFIELDS, http_build_query( $data ) );
    }

    $res=(object)array(
        'response'  =>  curl_exec( $curl ),
        'info'      =>  curl_getinfo( $curl ),
        'errors'    =>  curl_error( $curl ),
        'cookie'    =>  $cookiejar
    );
    curl_close( $curl );
    return $res;
}




/* configure and call the function - used the suggestion from @Thomas for the endpoint url  */
$url='https://www.maxmind.com/en/download_files';
$data=array(
    'login'         =>  $username,  /* where $username & $password are YOUR variables */
    'password'      =>  $password,
    'pkit_login'    =>  1,
    'pkit_done'     =>  '/en/download_files',
    'Login'         =>  'Login'
);
$options=(object)array(
    'post'  =>true,
    'cookie'=>true
);

$res=mmcurl( $url, $data, $options );

pre($res->info);

因为我没有帐户,所以我无法进一步测试,但使用虚拟用户名/密码,响应是401错误,似乎错误的用户名和密码。

相关问题