Reputation: 10748
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. :(');
}
Upvotes: 2
Views: 8323
Reputation: 360762
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");
Upvotes: 4