2015-09-14 34 views
0

我正在嘗試連接hubstaff api,有沒有人試過它?我是php-cURL的新手,你如何將它轉換爲PHP Curl?Hubstaff - 用php檢索數據cURL

curl -H "App-Token: BMyQnju-4tknuBQMsN0ujr6NWF5ohQaP9de8AWMJXik" -H "Auth-Token: X-vfv2c7jf_0NKoHLbX1t4yftK-TI-jZ4d7roNegw24" "http://api.hubstaff.com/v1/users" 

它也沒有表現出任何結果我做到這一點:

// Standard data 
    $data['app_token'] = $this->app_token; 

    // Debugging output 
    $this->debug = array(); 
    $this->debug['HTTP Method'] = $http_method; 

    // Create a cURL handle 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     'App-Token: ' . $this->app_token, 
     'Content-Type: application/xml' 
    )); 


    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 

    // Send data 
    if (!empty($data)) { 

     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); 

     // Debugging output 
     $this->debug['Posted Data'] = $data; 
    } 

    // Execute cURL request 
    $curl_response = curl_exec($ch); 

    // Save CURL debugging info 
    $this->debug['Last Response'] = $curl_response; 
    $this->debug['Curl Info'] = curl_getinfo($ch); 

    // Close cURL handle 
    curl_close($ch); 

    // Parse response 
    $response =$curl_response;// $this->parseAsciiResponse($curl_response); 

    // Return parsed response 
    return $response; 

我只是試圖讓我的身份驗證令牌

任何幫助將不勝感激。

+0

你已經解決了嗎?我有類似的問題。 –

+0

@MichalOlszowski是的,我解決了這個問題,並在下面發佈了答案。 –

回答

0

@Michal我已經解決了我自己的問題,並創建了這個簡單的類來幫助其他人快速連接hubstaff。隨時爲您只需使用任何建議和優化

class HubstaffApi { 

private $app_token = ''; 
private $auth_token = ''; 

public function __construct($app_token, $auth_token) { 
    $this->app_token = $app_token; 
    $this->auth_token = $auth_token; 
} 

private function sendRequest($api_method, $http_method = 'GET', $data = null) { 
    // Standard data 
    $data['app_token'] = $this->app_token; 
    $request_url = "https://api.hubstaff.com/v1/"; 

    // Debugging output 
    $this->debug = array(); 
    $this->debug['Request URL'] = $request_url . $api_method; 

    // Create a cURL handle 
    $ch = curl_init(); 

    // Set the request 
    curl_setopt($ch, CURLOPT_URL, $request_url . $api_method); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     'App-Token: ' . $this->app_token, 
     'Auth-Token: ' . $this->auth_token 
    )); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $http_method); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 

    // Send data 
    if (!empty($data)) { 

     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); 

     // Debugging output 
     $this->debug['Posted Data'] = $data; 
    } 

    // Execute cURL request 
    $curl_response = curl_exec($ch); 

    // Save CURL debugging info 
    $this->debug['Last Response'] = $curl_response; 
    $this->debug['Curl Info'] = curl_getinfo($ch); 

    // Close cURL handle 
    curl_close($ch); 

    // Parse response 
    $response = $curl_response; 

    // Return parsed response 
    return $response; 
} 

public function users(array $parameters = array()) { 
    return $this->sendRequest('users', 'GET', $parameters); 
} 

public function activities(array $parameters = array()) { 
    return $this->sendRequest('activities', 'GET', $parameters); 
} 

public function screenshots(array $parameters = array()) { 
    return $this->sendRequest('screenshots', 'GET', $parameters); 
} 

} 

$Hubstaff = new HubstaffApi(
     YOUR_APP_TOKEN, 
YOUR_AUTH_TOKEN); //simply get auth token in developer.hubstaff 's generator, it doesn't expire anyway. 

$response = $Hubstaff->activities([ 
    "start_time" => "2015-09-10T00:00:00+08:00:00", 
    "stop_time" => "2015-09-10T24:00:00+08:00:00", 
    "users" => YOUR_HUBSTAFF_ID 
]); 

    echo $response;