1 Reply Latest reply on Jul 20, 2017 5:40 PM by APIcoder

    Web publishing PHP coding question

    edwardlscott

      I'm working on a solution for a client.  Flow: A mass-mailing script sends out formatted html emails using a handy plugin.  The html includes links intended for recipients to click and review the web content of the links (standard web pages).

       

      The links embedded in the html email are not directly to the web pages, but to a PHP script on an FM Server (passes in a parameter).  In the PHP script, it redirects the user back to their link of interest.  The rest of the PHP script writes a record to a FMS hosted database.  All this is working nicely.

       

      I'm pretty new with PHP code.  I am trying to use the last part of my PHP script to write out a textual log file (on the FM server) immediately after the command->execute step that calls the FileMaker script in my solution file.  Intent: It should do one thing if the "command->execute" successfully runs and another if it encounters any errors.

       

      Here is my PHP code beginning with the command that executes the FileMaker-side script (the execute works fine):

       

      $result = $command->execute();

       

      // write a log file of errors and successes

      $timestamp = date("m/d/Y")." @ ".date("h:i:sa");

       

      If(!empty($result)){

      $logfile = fopen("Log_PHP_TXA_Responder.txt", "a+") or die("Unable to open file!");

      fwrite($logfile, $timestamp.": ".$result."\r\r");  // Writes out any error message in $result

      fclose($logfile);

      exit;

      }

      // Beyond here is the problem, I cannot get this to execute for the life of me

      $logfile = fopen("Log_PHP_TXA_Responder.txt", "a+") or die("Unable to open file!");

      fwrite($logfile, $timestamp.": Successful update for ".$campaign.", ".$aianum.", ".$urlclick."\r\r");

      fclose($logfile);

       

      exit;

       

      When there is an error (like say I change the execute-context-layout name to have a typo in it) the first If statement correctly writes my message to the log file (then exits as intended). If there is no error, I would expect the first IF statement to get skipped and for last four lines to execute instead.  I've tried wrapping them in If statements that are always true and everything I can think of, it seems like the PHP script simply stops executing after the curly bracket at the end of the first If statement.

       

      Any help would be much appreciated.

       

      Thanks in advance,

      Ed

        • 1. Re: Web publishing PHP coding question
          APIcoder

          Wel $result may always have a value and meets the criteria, "If(!empty($result)){"

           

          Better to use Something like this:

           

          if (FileMaker::isError($result)) {

              if (!isset($result->code) || strlen(trim($result->code)) < 1) {

                  writeToLog('A System Error Occured');

                  die;

              } else {

                  writeToLog('Error code: ' . $result->code .' Message: '. $result->message );

                  die;

              }

          } else {

           

              /* No Error */

              $records = $result->getRecords();

              $foundCount = $result->getFetchCount();

             

              if(  $foundCount > 0 ) {

                  foreach ($records as $record) {

                      echo $record->getField( "nameFirst" );

                  }

              }

          }

           

           

          function writeToLog($error) {

              $logfile = fopen("Log_PHP_TXA_Responder.txt", "a+") or die("Unable to open file!");

              fwrite($logfile, $timestamp.": ".$error."\r\r");  // Writes out any error message in $result

              fclose($logfile);

          }