PHP 卷曲头

发布于 2024-12-24 22:11:33 字数 2338 浏览 1 评论 0原文

我正在尝试将 cURL 与 freshbooks API 结合使用。它有两种身份验证方式:OpenAuth 和基于令牌。我正在尝试使用基于令牌的方法。我以前使用过 cURL,但我的身份验证凭据始终位于传递的 xml 中。

对于 freshbooks,我似乎需要在 cURL 请求的标头中传递凭据......我认为。下面是一些使用 OpenAuth 方法的示例代码。使用基于令牌的身份验证,我只需要传递用户名和密码。

如何通过 cURL 请求正确传递我的凭据?

http://developers.freshbooks.com/authentication-2/#TokenBased

private function buildAuthHeader()
    {
        $params = array(
            'oauth_version' => '1.0',
            'oauth_consumer_key' => $this->oauth_consumer_key,
            'oauth_token' => $this->oauth_token,
            'oauth_timestamp' => time(),
            'oauth_nonce' => $this->createNonce(20),
            'oauth_signature_method' => 'PLAINTEXT',
            'oauth_signature' => $this->oauth_consumer_secret. '&' .$this->oauth_token_secret
        );

        $auth = 'OAuth realm=""';
        foreach($params as $kk => $vv)
        {
            $auth .= ','.$kk . '="' . urlencode($vv) . '"';
        }

        return $auth;
    }


    public function post($request)
    {
        $this->fberror = NULL;
        $headers = array(
                    'Authorization: '.$this->buildAuthHeader().'',
                    'Content-Type: application/xml; charset=UTF-8',
                    'Accept: application/xml; charset=UTF-8',
                    'User-Agent: My-Freshbooks-App-1.0');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->apiUrl());
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

        $response = curl_exec($ch);
        curl_close($ch);
        $response = new SimpleXMLElement($response);

        if($response->attributes()->status == 'ok') 
            return $response;
        else if($response->attributes()->status == 'fail' || $response->fberror)    
            throw new FreshbooksAPIError($response->error);
        else throw new FreshbooksError('Oops, something went wrong. :(');
    }

I'm trying to wrap my head around using cURL with the freshbooks API. It has two ways of authentication: OpenAuth and token based. I'm trying to use the token based method. I've used cURL before but my authentication credentials have always been in the xml being passed.

With freshbooks, it seems i need to pass the credentials in the header of the cURL request...i think. Below is some example code using the OpenAuth method. Using the token based authentication, i am only suppose to pass a username and password.

How do i properly pass my credentials with my cURL request?

http://developers.freshbooks.com/authentication-2/#TokenBased

private function buildAuthHeader()
    {
        $params = array(
            'oauth_version' => '1.0',
            'oauth_consumer_key' => $this->oauth_consumer_key,
            'oauth_token' => $this->oauth_token,
            'oauth_timestamp' => time(),
            'oauth_nonce' => $this->createNonce(20),
            'oauth_signature_method' => 'PLAINTEXT',
            'oauth_signature' => $this->oauth_consumer_secret. '&' .$this->oauth_token_secret
        );

        $auth = 'OAuth realm=""';
        foreach($params as $kk => $vv)
        {
            $auth .= ','.$kk . '="' . urlencode($vv) . '"';
        }

        return $auth;
    }


    public function post($request)
    {
        $this->fberror = NULL;
        $headers = array(
                    'Authorization: '.$this->buildAuthHeader().'',
                    'Content-Type: application/xml; charset=UTF-8',
                    'Accept: application/xml; charset=UTF-8',
                    'User-Agent: My-Freshbooks-App-1.0');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->apiUrl());
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

        $response = curl_exec($ch);
        curl_close($ch);
        $response = new SimpleXMLElement($response);

        if($response->attributes()->status == 'ok') 
            return $response;
        else if($response->attributes()->status == 'fail' || $response->fberror)    
            throw new FreshbooksAPIError($response->error);
        else throw new FreshbooksError('Oops, something went wrong. :(');
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随遇而安 2024-12-31 22:11:33

链接页面中的 curl -u 命令行示例只是使用 HTTP 基本身份验证,其中令牌是用户名。等效的 phpcurl 是

curl_setopt($ch, CURLOPT_USERPWD, "$token:$password");

The curl -u command line example in your linked page is simply using HTTP Basic authentication where the token is the username. The equivalent phpcurl would be

curl_setopt($ch, CURLOPT_USERPWD, "$token:$password");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文