PHP 卷曲头
我正在尝试将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
链接页面中的
curl -u
命令行示例只是使用 HTTP 基本身份验证,其中令牌是用户名。等效的 phpcurl 是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