我正在尝试创建一个 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