douzhan8652 2015-09-14 04:30
浏览 32
已采纳

Hubstaff - 使用php cURL检索数据

I'm trying to connect with hubstaff api, has anyone ever tried it? I'm a newbie in php-cURL, how do you convert this to PHP Curl?

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

It also would not show any result of I do this:

// 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;

Im just trying to get my Auth-Token

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duanlei4759 2015-10-06 08:53
    关注

    @Michal I have solved my own problem and created this simple class to help anyone else in connecting with hubstaff fast. feel free for any suggestions and optimizations

    class HubstaffApi {
    
    private $app_token = '';
    private $auth_token = '';
    private $debug = [];
    
    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);
    }
    
    }
    

    You can simply use this by:

    $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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测