· 7 years ago · Aug 08, 2018, 02:20 AM
1php curl header
2private function buildAuthHeader()
3 {
4 $params = array(
5 'oauth_version' => '1.0',
6 'oauth_consumer_key' => $this->oauth_consumer_key,
7 'oauth_token' => $this->oauth_token,
8 'oauth_timestamp' => time(),
9 'oauth_nonce' => $this->createNonce(20),
10 'oauth_signature_method' => 'PLAINTEXT',
11 'oauth_signature' => $this->oauth_consumer_secret. '&' .$this->oauth_token_secret
12 );
13
14 $auth = 'OAuth realm=""';
15 foreach($params as $kk => $vv)
16 {
17 $auth .= ','.$kk . '="' . urlencode($vv) . '"';
18 }
19
20 return $auth;
21 }
22
23
24 public function post($request)
25 {
26 $this->fberror = NULL;
27 $headers = array(
28 'Authorization: '.$this->buildAuthHeader().'',
29 'Content-Type: application/xml; charset=UTF-8',
30 'Accept: application/xml; charset=UTF-8',
31 'User-Agent: My-Freshbooks-App-1.0');
32
33 $ch = curl_init();
34 curl_setopt($ch, CURLOPT_URL, $this->apiUrl());
35 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
36 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
37 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
38 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
39 curl_setopt($ch, CURLOPT_POST, 1);
40 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
41
42 $response = curl_exec($ch);
43 curl_close($ch);
44 $response = new SimpleXMLElement($response);
45
46 if($response->attributes()->status == 'ok')
47 return $response;
48 else if($response->attributes()->status == 'fail' || $response->fberror)
49 throw new FreshbooksAPIError($response->error);
50 else throw new FreshbooksError('Oops, something went wrong. :(');
51 }
52
53curl_setopt($ch, CURLOPT_USERPWD, "$token:$password");