moonshine
moonshine

Reputation: 859

How to use Curl to Post to Miscrosoft Teams channnel

I am trying to send a simple message to a team channel, and here's what I have tried:

$link = 'My-Link-Goes-Here';
$curl = curl_init($link);

$postfields = array(
    'text' => 'HELLO',
);
$postfields = json_encode($postfields);

curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($curl);
var_dump($result);
curl_close($curl);

what did i've done wrong ?

Upvotes: 0

Views: 3823

Answers (1)

moonshine
moonshine

Reputation: 859

I answer to myself, if it can help anyone :

function Webhook($Name){
    $url = 'My Url Goes Here';

    $ch = curl_init();

    $jsonData = array(
        'text' => 'Hello '.$Name.' !!'
    );
    $jsonDataEncoded = json_encode($jsonData, true);


    $header = array();
    $header[] = 'Content-type: application/json';


    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    $result = curl_exec($ch);
    curl_close($ch);

    var_dump($result);
}

Upvotes: 3

Related Questions