我正在使用以下 php 脚本来访问 Fitbit API。问题是当我在本地主机上运行脚本时它工作正常,但是当我在远程服务器上运行它时,它给出了以下异常:
OAuthException Object ( [message:protected] => making the request failed (dunno why)
[string:Exception:private] => [code:protected] => -1 [file:protected] =>
/net/www/example.com/htdocs/exampleFitbit.php [line:protected] => 41
[trace:Exception:private] => Array ( [0] => Array ( [file] =>
/net/www/example.com/htdocs/exampleFitbit.php [line] => 41 [function] => getRequestToken
[class] => OAuth [type] => -> [args] => Array ( [0] => http://api.fitbit.com/oauth/request_token [1]
=> http://www.example.com/exampleFitbit.php ) ) ) [previous:Exception:private] =>
[lastResponse] => [debugInfo] => )
剧本:
<?php
$baseUrl = 'http://api.fitbit.com';
$req_url = $baseUrl . '/oauth/request_token';
$authurl = $baseUrl . '/oauth/authorize';
$acc_url = $baseUrl . '/oauth/access_token';
$conskey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$conssec = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$apiCall = "http://api.fitbit.com/1/user/-/profile.xml";
session_start();
if ( !isset($_GET['oauth_token']) && $_SESSION['state']==1
$_SESSION['state'] = 0;
try
{
$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauth->enableDebug();
if ( $_SESSION['state'] == 0 )
{
$request_token_info = $oauth->getRequestToken($req_url, 'http://example.com/exampleFitbit.php');
$_SESSION['secret'] = $request_token_info['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: '.$authurl.'?oauth_token='.$request_token_info['oauth_token']);
exit;
}
else if ( $_SESSION['state']==1 )
{
$oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
$access_token_info = $oauth->getAccessToken($acc_url);
$_SESSION['state'] = 2;
$_SESSION['token'] = $access_token_info['oauth_token'];
$_SESSION['secret'] = $access_token_info['oauth_token_secret'];
}
$oauth->setToken($_SESSION['token'],$_SESSION['secret']);
$oauth->fetch($apiCall);
$response = $oauth->getLastResponse();
$xml = simplexml_load_string($response);
print_r($xml);
}
catch( OAuthException $E )
{
print_r($E);
}
?>
如果有任何资源/教程/帮助/提示,请分享。