使用PHP cURL进行Toggl Reporting API

时间:2013-09-27 14:53:40

标签: php rest curl

我正在尝试访问Toggl Reporting API。

我尝试在PHP中使用cURL,后者连接到API,但是会出现以下错误消息:“可能不会使用此方法。”任何解释为什么会这样的情况都会有用,因为我对webservices很新。我可能会遗漏一些明显或完全错误的方法,所以如果是这种情况就道歉。

<?php
$userAgent = 'xxx';//username
$token = 'xxx';//token
$returned_content = get_data('https://toggl.com/reports/api/v2/summary?&workspace_id=[workspaceid]&since=2013-05-19&until=2013-05-20&user_agent=[username here]');
print_r($returned_content);

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_USERPWD, $token.':api_token');
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
?>

编辑:我尝试了不同的方法。如果我运行以下代码,我不再收到任何错误消息,因此代码似乎正在执行但我无法将响应打印到屏幕上。除了print_r之外,我还需要做些什么来查看输出吗?(Toggl API返回JSON)。感谢。

$ json = curl%20-v%20-u%[myapitoken]:api_token%20https://toggl.com/reports/api/v2/weekly?workspace_id=[id]&wsid=282507&since=2012-08-19&until=2013-09-20&user_agent=[user].json; 的print_r($ JSON);

编辑:终于解决了!代码如下:

$workspace_id = '[id here]';
$user_agent = '[user agent here]'; // no spaces
$api_token = '[token here]';
$report_url = 'https://toggl.com/reports/api/v2/weekly?user_agent='.$user_agent.'&since=2013-08-01&until=2013-09-01';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_USERPWD, $api_token . ':api_token');
curl_setopt($ch, CURLOPT_URL, $report_url . '&workspace_id=' . $workspace_id);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);

$result = json_encode($result);

希望这可以帮助将来的某个人!

1 个答案:

答案 0 :(得分:0)

据我了解,由于CURLOPT_SSL_VERIFYPEER == FALSE,您收到此消息。

尝试从代码中删除此字符串:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

也许我错了,但我认为使用此选项,您将从Toggl服务器收到“HTTP 501 Not Implemented”错误,该错误包含完全相同的消息“可能不使用此方法。”