TRIGGERcmd
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Would this PHP code work?

    Scheduled Pinned Locked Moved General Discussion
    2 Posts 2 Posters 1.2k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C Offline
      ctaylor
      last edited by ctaylor

      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!

      RussR 1 Reply Last reply Reply Quote 0
      • RussR Offline
        Russ @ctaylor
        last edited by Russ

        @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.

        Russell VanderMey

        1 Reply Last reply Reply Quote 0

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • First post
          Last post