Slack Slash 命令 - 使用用户名 (PHP) 获取用户图标 URL

Slack Slash Command - Getting user icon URL with username (PHP)

我不熟悉在 Slack 中编写 Slash 命令。对于我的一个命令,我有一个用户名并且需要检索用户图标 URL。我正在使用 PHP 对它们进行编码。

我正计划使用 users.profile.get,因为教程 here 显示返回的字段之一是用户图标 URL。

但是,我试图找到有关如何调用此方法的示例,但没有找到任何示例。谁能给我一个简单的调用示例,包括如何发送参数?

这是我的进展:

$slack_profile_url = "https://slack.com/api/users.profile.get";
$fields = urlencode($data);        
$slack_call = curl_init($slack_profile_url);
curl_setopt($slack_call, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($slack_call, CURLOPT_POSTFIELDS, $fields);
curl_setopt($slack_call, CURLOPT_CRLF, true);                                                               
curl_setopt($slack_call, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($slack_call, CURLOPT_HTTPHEADER, array(                                                                          
    "Content-Type: application/x-www-form-urlencoded",                                                                                
    "Content-Length: " . strlen($fields))                                                                       
);                                                                                                                   

$profile = curl_exec($slack_call);
curl_close($slack_call);

我基本上有$token和$user_name,需要获取头像URL。如何将 $token 和 $username 格式化为 $data?调用正确吗?

如果有人建议以不同的方式执行此操作,我也将不胜感激。

非常感谢!

将数据转换为 post 到 Slack 的正确格式非常简单。有两个选项(POST 正文或 application/x-www-form-urlencoded)。

application/x-www-form-urlencoded 的查询字符串格式类似于 get URL 字符串。

https://slack.com/api/users.profile.get?token={token}&user={user}
// Optionally you can add pretty=1 to make it more readable
https://slack.com/api/users.profile.get?token={token}&user={user}&pretty=1

只需请求 URL 即可检索数据。

POST 正文格式将使用与您上面的代码类似的代码。

$loc = "https://slack.com/api/users.profile.get";
$POST['token'] = "{token}";
$POST['user'] = "{user}";

$ch = curl_init($loc);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $POST);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     

$result = curl_exec($ch);
    if ($error = curl_errno($ch)) { echo $error; }

    //close connection
    curl_close($ch);

echo $result;