1

我正在尝试创建一个 PHP 脚本,该脚本会自动将<textarea>我的网络表单中的文本推送到 Slack 频道。

HTML:

<form action="http://main.xfiddle.com/<?php echo pf_file('g7f-ds0'); ?>" method="post" id="myform" name="myform">   
<textarea name="text" id="" rows="3" cols="30">
</textarea> <br /><br />
<button id="mysubmit" type="submit" name="submit">Submit</button><br /><br /></form>

我设法编写了一个 PHP 脚本,将硬编码消息发布到 Slack,如下所示:

    <?php

//API Url
$url = 'https://hooks.slack.com/services/T02NZ01FU/B08TTAPGE/000000000000000000';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$payload = array(
   ’text' => 'Testing text with PHP'
);

//Encode the array into JSON.
$jsonDataEncoded = json_encode($payload);

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

//Execute the request
$result = curl_exec($ch);
?>

但是由于某种原因,当我尝试从中获取文本<textarea name="text" rows="3" cols="30"></textarea>并将其保存到变量中时,它就不起作用了。我将此添加到 PHP 的开头以设置文本变量:

if(isset($_POST['submit']))
$textdata = $_POST['text'];

然后将 $payload 更改为

'text' => $textdata
4

2 回答 2

1

如何使用 slack 传入 webhook 的简单示例curl

<?php
define('SLACK_WEBHOOK', 'https://hooks.slack.com/services/xxx/yyy/zzz');
function slack($txt) {
  $msg = array('text' => $txt);
  $c = curl_init(SLACK_WEBHOOK);
  curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($c, CURLOPT_POST, true);
  curl_setopt($c, CURLOPT_POSTFIELDS, array('payload' => json_encode($msg)));
  curl_exec($c);
  curl_close($c);
}
?>

这里截取的片段

于 2017-06-25T13:29:19.170 回答
0

这里有两个可能的问题。

  1. 您帖子中的 PHP 格式不正确。

    替换’text' => 'Testing text with PHP''text' => 'Testing text with PHP'

  2. 您的 curl 设置不正确。请参阅以下帖子以调试 curl并修复可能出现的错误 -没有受信任的 SSL 证书

于 2015-09-04T21:19:15.537 回答