在远程主机上使用PHP OAuth扩展运行PHP脚本。

时间:2013-11-12 16:29:24

标签: php oauth pecl

我使用以下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);
        }
    ?>

如果有任何资源/教程/帮助/提示,​​请分享。

1 个答案:

答案 0 :(得分:0)

您可以尝试在远程主机上使用pcap / wireshark来捕获网络流量,但我怀疑如果您不是root用户,这将无效。

因此,您唯一的解决方案是向fitbit支持人员寻求帮助。

除此之外,您可以尝试通过wget或curl简单地获取profile.xml文件,看看是否有效。如果没有,则可能是防火墙问题。