Would this PHP code work?
-
Hi,
Been trying this PHP code and it won't work at all. Tried putting error reporting on and nothing is being thrown up.
Am I doing anything wrong?
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.triggercmd.com/api/run/triggerSave");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{"computer":"FR-CCTV-ANALYSIS","trigger":"armed"}");
curl_setopt($ch, CURLOPT_POST, 1);$headers = array();
$headers[] = "Authorization: Bearer API Token removed";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);?>
Thanks a lot if anyone can give some insight!
-
@ctaylor, you were so close! I made it work with a couple small tweaks. Here's my code that works.
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://www.triggercmd.com/api/run/triggerSave"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, '{"computer":"DS","trigger":"calculator"}'); curl_setopt($ch, CURLOPT_POST, 1); $headers = array(); $headers[] = "Authorization: Bearer (token from instructions page)"; $headers[] = "Content-Type: application/json"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close ($ch); ?>
You'll see that I changed this line to use single quotes around the json object.
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"computer":"DS","trigger":"calculator"}');
And I changed this line to use application/json instead of application/x-www-form-urlencoded because you're passing a json object.
$headers[] = "Content-Type: application/json";
In case you're curious, this is how I tested my test.php script:
docker run -it --rm --name test-script -v d:\appdev\php:/myscript -w /myscript php:7.0-cli php test.php
BTW, I'm curious what your application is.