Garmin Connect登录php

时间:2014-03-22 22:02:20

标签: php authentication curl

尝试使用新的SSO方法对garmin连接进行身份验证,但我一直陷入困境。我在python中找到了我认为的工作方法:

https://github.com/cpfair/tapiriik/commit/1604a1c791def072a48f3165dd61d5136f3610ce

到目前为止我的代码是:

$url = "https://sso.garmin.com/sso/login?service=http%3A%2F%2Fconnect.garmin.com%2Fpost-auth%2Flogin&webhost=olaxpw-connect04.garmin.com&source=http%3A%2F%2Fconnect.garmin.com%2Fen-US%2Fsignin&redirectAfterAccountLoginUrl=http%3A%2F%2Fconnect.garmin.com%2Fpost-auth%2Flogin&redirectAfterAccountCreationUrl=http%3A%2F%2Fconnect.garmin.com%2Fpost-auth%2Flogin&gauthHost=https%3A%2F%2Fsso.garmin.com%2Fsso&locale=en_US&id=gauth-widget&cssUrl=https%3A%2F%2Fstatic.garmincdn.com%2Fcom.garmin.connect%2Fui%2Fcss%2Fgauth-custom-v1.0-min.css&clientId=GarminConnect&rememberMeShown=true&rememberMeChecked=false&createAccountShown=true&openCreateAccount=false&usernameShown=true&displayNameShown=false&consumeServiceTicket=false&initialFocus=true&embedWidget=false";
$page = file_get_contents($url);
//echo $page;
   $username = '********';
$password = '************';

preg_match("/flowExecutionKey:\s\[(.*)-->/",$page,$m);
$flow = trim(str_replace(']','',$m[1]));
$ch = curl_init('https://sso.garmin.com/sso/login?service=http://connect.garmin.com/post-auth/login&clientID=GarminConnect&consumeServiceTicket=false');
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_ENCODING, "gzip" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, $cookie_file );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie_file );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
$query = http_build_query(array('username'=>$username,'password'=>$password,'embed'=>'true','_eventID'=>'submit','lt'=>$flow));
curl_setopt($ch, CURLOPT_POSTFIELDS,$query);
$output = curl_exec($ch);
$http_status = curl_getinfo($url, CURLINFO_HTTP_CODE);
curl_close($ch);
var_dump($http_status);
var_dump($output);

我能够毫无问题地获得流程执行密钥,但是当我将其与必填字段一起发布时,我根本没有输出...

1 个答案:

答案 0 :(得分:1)

首先尝试从代码中删除此行并再次运行。您在此处再次设置上一个网址。所以擦掉它。

curl_setopt( $ch, CURLOPT_URL, $url );

如果仍然不起作用:那么......

您正在使用$page = file_get_contents($url);浏览登录页面,因此在此次通话期间,网站上的任何Cookie都不会存储在您的$cookie_file中。

因此,不是调用file_get_contents函数,而是使用curl也使用相同的cookie文件获取html。

$login = curl_init();
curl_setopt( $login, CURLOPT_URL, $url );
curl_setopt( $login, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $login, CURLOPT_ENCODING, "" );
curl_setopt( $login, CURLOPT_COOKIEFILE, $cookie_file );
curl_setopt( $login, CURLOPT_COOKIEJAR, $cookie_file );
curl_setopt( $login, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt( $login, CURLOPT_SSL_VERIFYPEER,false);
$page= curl_exec($login);
curl_close($login);

如果仍然无效,请在第二次卷曲调用时添加此行。

curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER,false);

最后使用curl_setopt( $ch, CURLOPT_VERBOSE,true);看看你自己调试curl做什么。