首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Slack Slash命令-使用用户名获取用户图标URL (PHP)

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

Stack Overflow用户
提问于 2017-07-04 13:30:09
回答 1查看 828关注 0票数 0

我是个用Slack编程斜杠命令的新手。对于我的一个命令,我有一个用户名,并且需要检索用户图标URL。我正在使用PHP对它们进行编码。

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

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

这就是我得到的结果:

代码语言:javascript
复制
$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,并需要获得个人资料图片网址。如何将$token和$username格式化为$data?呼叫是否正确?

如果有人建议用不同的方式来做这件事,我也将非常感谢任何建议。

非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-05 10:02:20

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

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

代码语言:javascript
复制
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,您就可以检索数据。

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

代码语言:javascript
复制
$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;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44897625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档