3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* * CPMObjectEventHandler: BLCampaignsCreate * Package: RN * Objects: BLDialogue\BLCampaigns * Actions: Create,Update * Version: 1.2 */ // This object procedure binds to v1_1 of the Connect PHP API use RightNow\Connect\v1_2 as RNCPHP; // This object procedure binds to the v1 interface of the process // designer use \RightNow\CPM\v1 as RNCPM; /* Campaign create incident, when incident creating, will trigger this handler and send sms. */ /** * An Object Event Handler must provide two classes: * - One with the same name as the CPMObjectEventHandler tag * above that implements the ObjectEventHandler interface. * - And one of the same name with a "_TestHarness" suffix * that implements the ObjectEventHandler_TestHarness interface. * * Each method must have an implementation. */ class BLCampaignsCreate implements RNCPM\ObjectEventHandler { public static $apiUrl = 'https://dialogue.blueleap.com'; public static function apply($run_mode, $action, $obj, $n_cycles) { $actionToPerform = ''; date_default_timezone_set("UTC"); try { if (RNCPM\ActionCreate == $action) { if ($obj->StatusOrExceptionReason == 'SchedulePending') { self::scheduleCampaign($obj, 'schedule'); } else if ($obj->StatusOrExceptionReason == 'Executing') { self::executeCampaign($obj); } } else if (RNCPM\ActionUpdate == $action) { if ($obj->StatusOrExceptionReason == 'UpdatePending') { self::scheduleCampaign($obj, 'update'); } else if ($obj->StatusOrExceptionReason == 'CancelPending') { self::scheduleCampaign($obj, 'cancel'); } else if ($obj->StatusOrExceptionReason == 'Executing') { self::executeCampaign($obj); } } } catch (\Exception $ex) { $obj->StatusOrExceptionCode = 'ble1016'; $obj->StatusOrExceptionReason = 'Failed'; $obj->StatusOrExceptionDescription = 'General script failure - ' . $ex->getMessage(); $obj->save(); echo $ex->getMessage(); } return; } // apply() public static function scheduleCampaign($obj, $action) { $query = "select BLDialogue.BLAccount from BLDialogue.BLAccount"; $result = RNCPHP\ROQL::queryObject($query)->next(); $BLAccount = $result->next(); $tokenResponse = self::getToken($obj, $BLAccount->BLAccountSID, $BLAccount->BLAccountPWD); $tokenArr = json_decode($tokenResponse, true); $BLAccount->BLToken = $tokenArr['token']; $campaignResponse = self::scheduleCampaignServer($obj, $BLAccount, $action); if (strlen($campaignResponse)) { if (($campaignResult = json_decode($campaignResponse))) { $obj->StatusOrExceptionDescription = $campaignResult->StatusMessage; $obj->StatusOrExceptionReason = $campaignResult->RequestStatus; $obj->StatusOrExceptionCode = $campaignResult->RequestStatusCode; } else { // garbage response no json $obj->StatusOrExceptionCode = 'ble1014'; $obj->StatusOrExceptionReason = 'Failed'; $obj->StatusOrExceptionDescription = 'Garbage response from API not a json object.'; } } else { // no response from API $obj->StatusOrExceptionCode = 'ble1013'; $obj->StatusOrExceptionReason = 'Failed'; $obj->StatusOrExceptionDescription = 'No response from API.'; // sms send failed, set status failed and add proper reason } $obj->save(); } public static function executeCampaign($obj) { $obj->StartTime = time(); //$query = "select MarketingSettings.ContactLists.* from Contact limit 1"; $query = "SELECT Contact FROM Contact where MarketingSettings.ContactLists.NamedIDList=" . $obj->ContactListID; //$query = "SELECT Contact FROM Contact where MarketingSettings.ContactLists.NamedIDList=2408"; $result = RNCPHP\ROQL::queryObject($query)->next(); $obj->OptOutCount = 0; //$obj->SMSCreateCount = 0; //$obj->SMSFailedCount = 0; $obj->AudienceCount = 0; $campaign = RNCPHP\BLDialogue\BLCampaigns::fetch($obj->ID); while ($contact = $result->next()) { $campaign = RNCPHP\BLDialogue\BLCampaigns::fetch($obj->ID); if ($campaign->StatusOrExceptionReason == 'Executing') { if ($contact->CustomFields->BLDialogue->sms_opt_out) { $obj->OptOutCount++; } $optedOut = false; if ($contact->CustomFields->BLDialogue->sms_opt_out && $obj->OptOutOverride) $optedOut = false; else if ($contact->CustomFields->BLDialogue->sms_opt_out) $optedOut = true; if (self::saveBlOutboundMessage($contact, $obj, $optedOut)) { //$obj->SMSCreateCount++; } else { //$obj->SMSFailedCount++; } $obj->AudienceCount++; } else { break; } } if ($campaign->StatusOrExceptionReason == 'Executing') { $obj->StatusOrExceptionCode = 'bls1029'; $obj->StatusOrExceptionReason = 'Finished'; $obj->StatusOrExceptionDescription = 'Campaign has finished processing'; } $obj->FinishTime = time(); $obj->save(); } public static function saveBlOutboundMessage($contact, $BlCampaigns, $optedOut) { try { $messageBody = self::getMessageBody($BlCampaigns->MessageBody, $contact); $BlOutboundMessage = new RNCPHP\BLDialogue\BLOutboundMessage(); $BlOutboundMessage->MessageBody = $messageBody; $BlOutboundMessage->AlphaNumeric = $BlCampaigns->AlphaNumeric; $BlOutboundMessage->ContactID = $contact; //$BlOutboundMessage->MessageSID = $messageCount; $BlOutboundMessage->AgentID = $BlCampaigns->AgentID; $BlOutboundMessage->RunTime = $BlCampaigns->RunTime; $BlOutboundMessage->ContactGMToffset = $BlCampaigns->ContactGMToffset; $BlOutboundMessage->MessageType = $BlCampaigns->MessageType; $roql_result_set = RNCPHP\ROQL::query("SELECT Phones.Number from Contact where Phones.PhoneType.ID = 1 and ID=" . $contact->ID)->next()->next(); if (($number = self::formatNumber($roql_result_set['Number'])) === false) { $number = $roql_result_set['Number']; $BlOutboundMessage->StatusOrExceptionCode = 'ble1020'; $BlOutboundMessage->StatusOrExceptionReason = 'Failed'; $BlOutboundMessage->StatusOrExceptionDescription = 'You have attempted to send an SMS with a number that is not a valid mobile number'; // if(($number = self::formatNumber($contact->Phones[0]->Number))===false) // { // $number = $contact->Phones[1]->Number?$contact->Phones[1]->Number:$contact->Phones[0]->Number; // $BlOutboundMessage->StatusOrExceptionCode = 'ble1020'; // $BlOutboundMessage->StatusOrExceptionReason ='Failed'; // } } if ($number == "dnp") { $number = $roql_result_set['Number']; } $BlOutboundMessage->ContactToNumber = $number; //$BlOutboundMessage->ContactFirstName = $contact->Name->First; //$BlOutboundMessage->ContactLastName = $contact->Name->Last; //$BlOutboundMessage->ContactCountry = $contact->Address->Country->Name; //$BlOutboundMessage->ContactState = $contact->Address->StateOrProvince->LookupName; $BlOutboundMessage->CampaignID = $BlCampaigns->ID; $BlOutboundMessage->OptOutOverride = $BlCampaigns->OptOutOverride; //$BlOutboundMessage->MessageDirection = 'Outbound'; //print_r($BlOutboundMessage);exit; if ($optedOut) { $BlOutboundMessage->StatusOrExceptionCode = 'BLE1019'; $BlOutboundMessage->StatusOrExceptionReason = 'Failed'; $BlOutboundMessage->StatusOrExceptionDescription = 'Customer has opted out. SMS channel not available.'; } //echo 'code - '.$BlOutboundMessage->StatusOrExceptionCode.' Number - '.$BlOutboundMessage->ContactToNumber; $BlOutboundMessage->save(); return true; } catch (\Exception $ex) { echo $ex->getMessage(); return false; } } function scheduleCampaignServer($BLCampaign, $BLAccount, $action) { try { $url = self::$apiUrl . '/api/v1/campaigns/'; $headers = array( "Cache-Control: no-cache", "Pragma: no-cache", "Authorization: " . $BLAccount->BLToken ); $context = array( "BLAccountSID" => $BLAccount->BLAccountSID, "Action" => $action, "CustomerDomain" => $BLAccount->CustomerDomain1, "RunTime" => $BLCampaign->RunTime ? gmdate("m/d/Y h:i:s a", $BLCampaign->RunTime) : null, "CampaignID" => $BLCampaign->ID, ); $postargs = http_build_query($context); // Get the curl session object load_curl(); $session = curl_init(); curl_setopt($session, CURLOPT_HTTPHEADER, $headers); curl_setopt($session, CURLOPT_URL, $url); curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($session, CURLOPT_POST, true); curl_setopt($session, CURLOPT_POSTFIELDS, $postargs); curl_setopt($session, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($session, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($session, CURLOPT_FOLLOWLOCATION, 1); // Do the POST and then close the session $response = curl_exec($session); $httpcode = curl_getinfo($session); curl_close($session); return $response; } catch (\Exception $ex) { echo $ex->getMessage(); self::apiRequestTimeout($BLCampaign); //throw new RNCPM\CPMException($ex->getMessage(), 500); } } function getToken($BLCampaign, $username, $password) { try { $headers = array( "Cache-Control: no-cache", "Pragma: no-cache", 'Accept: application/json', 'Content-Type: apploication/json', "Authorization: Basic " . base64_encode($username . ":" . $password) ); load_curl(); $session = curl_init(); curl_setopt($session, CURLOPT_HTTPHEADER, $headers); curl_setopt($session, CURLOPT_URL, self::$apiUrl . '/api/v1/token'); curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($session, CURLOPT_POST, false); curl_setopt($session, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($session, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($session, CURLOPT_FOLLOWLOCATION, 1); $response = curl_exec($session); curl_close($session); return $response; } catch (\Exception $ex) { echo $ex->getMessage(); self::apiRequestTimeout($BLCampaign); //throw new RNCPM\CPMException($ex->getMessage(), 500); } } function apiRequestTimeout($BLCampaign) { $BLCampaign->StatusOrExceptionCode = 'ble1015'; $BLCampaign->StatusOrExceptionReason = 'Failed'; $BLCampaign->StatusOrExceptionDescription = 'API request timeout.'; $BLCampaign->save(); } function getStatusReasonObj($type, $name) { $query = "select " . $type . " from " . $type . " where Name ='" . $name . "'"; $result = RNCPHP\ROQL::queryObject($query)->next(); return $result->next(); } function getMessageBody($messageBody, $contact) { // if($BlCampaigns->MergeFirstName) // { // $messageBody = str_replace('[FIRSTNAME]',$contact->Name->First,$messageBody); // } // if($BlCampaigns->MergeStudentID) // { // $messageBody = str_replace('[STUDENTID]',$data->CustomFields->c->student_id,$messageBody); // } $pattern = "!(?<=[[])[^]]+(?=[]])!"; preg_match_all($pattern, $messageBody, $match); $replace = array(); $i = 0; foreach ($match[0] as $field) { try { $query = 'select Contact.' . $field . ' from Contact where ID=' . $contact->ID; $result = RNCPHP\ROQL::query($query)->next()->next(); $replace[$i] = end($result); } catch (\Exception $ex) { $replace[$i] = ''; } $match[0][$i] = '[' . $field . ']'; $i++; } $messageBody = str_replace($match[0], $replace, $messageBody); return $messageBody; } function formatNumber($number) { $phoneNumber = preg_replace('/[^0-9+]/', '', $number); $query = "select BLDialogue.BLAccount from BLDialogue.BLAccount"; $result = RNCPHP\ROQL::queryObject($query)->next(); $BLAccount = $result->next(); if (substr($phoneNumber, 0, 1) == '+') { return $phoneNumber; } else if (substr($phoneNumber, 0, strlen($BLAccount->LocalCountryCode)) == $BLAccount->LocalCountryCode) { return '+' . $phoneNumber; } if ($BLAccount->LocalCountryCode == "61") { if (substr($phoneNumber, 0, 4) == "0011") { return str_replace("0011", '+', $phoneNumber); } // else if((strlen($phoneNumber)==11 || strlen($phoneNumber)==12) && substr($phoneNumber,0,1)!='+') // { // return '+'.$phoneNumber; // } else if (strlen($phoneNumber) == 8) { return false; } // else if((strlen($phoneNumber)==10 && (!in_array(substr($phoneNumber, 0, 2),["04","05"]))) || (strlen($phoneNumber)==9 && (!in_array(substr($phoneNumber, 0, 1),["4","5"]))) ) // { // return false; // } else if ((strlen($phoneNumber) == 10 && in_array(substr($phoneNumber, 0, 2), ["04", "05"])) || (strlen($phoneNumber) == 9 && in_array(substr($phoneNumber, 0, 1), ["4", "5"]) )) { $phoneNumber = strlen($phoneNumber) == 10 ? substr($phoneNumber, 1) : $phoneNumber; return '+61' . $phoneNumber; } else if ((strlen($phoneNumber) == 10 && in_array(substr($phoneNumber, 0, 2), ["02", "03", "07", "08"])) || (strlen($phoneNumber) == 9 && in_array(substr($phoneNumber, 0, 2), ["2", "3", "7", "8"]))) { return false; } else if (strlen($phoneNumber) == 9) { return '+61' . $phoneNumber; } } else { if (substr($phoneNumber, 0, 1) == '0') $phoneNumber = substr($phoneNumber, 1); return '+' . $BLAccount->LocalCountryCode . $phoneNumber; } return false; } /* * *******End************ */ } // class incident_name /* The Test Harness */ class BLCampaignsCreate_TestHarness implements RNCPM\ObjectEventHandler_TestHarness { static $co_invented = NULL; public static function setup() { try { $query = "SELECT MarketingSettings.ContactLists.NamedIDList.ID FROM Contact limit 1"; $result = RNCPHP\ROQL::query($query)->next()->next(); if (isset($result['ID'])) { $contact = RNCPHP\Contact::first(); //echo "creating contact"; $BlCampaign = new RNCPHP\BLDialogue\BLCampaigns(); $BlCampaign->CampaignName = 'Event Handler Test Case'; //$BlCampaign->RunTime = strtotime('2017-12-12 12:00:00' ); $BlCampaign->ContactListID = $result['ID']; $BlCampaign->OptOutOverride = 0; $BlCampaign->AlphaNumeric = 0; //$BlCampaign->MergeStudentID = 0; //$BlCampaign->MergeFirstName = 0; $BlCampaign->AgentID = $contact; $BlCampaign->MessageBody = 'Event Handler Test Case Sample message'; $BlCampaign->MessageType = 'Message Campaign'; $BlCampaign->StatusOrExceptionCode = 'bls1022'; $BlCampaign->StatusOrExceptionReason = 'Executing'; $BlCampaign->StatusOrExceptionDescription = 'Camapign is being processed'; $BlCampaign->save(); static::$co_invented = $BlCampaign; } } catch (RNCPHP\ConnectAPIErrorFatal $err) { echo "Error in test harness save contact : " . $err->getCode(); } return; } public static function fetchObject($action, $object_type) { // Return the object that we // want to test with. // You could also return an array of objects // to test more than one variation of an object. //$obj = RNCPHP\BLDialogue\BLCampaigns::fetch(289); //return $obj; return static::$co_invented?static::$co_invented: array(); //return array(); } public static function validate($action, $object) { return(1); } public static function cleanup() { // Destroy every object invented // by this test. // Not necessary since in test // mode and nothing is committed, // but good practice if only to // document the side effects of // this test. if (static::$co_invented) { static::$co_invented->destroy(); static::$co_invented = NULL; } return; } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
7.4.00.0090.00914.97
7.3.120.0030.01514.92
7.3.110.0160.00315.07
7.3.100.0070.01014.56
7.3.90.0030.01214.95
7.3.80.0100.00714.83
7.3.70.0110.00014.93
7.3.60.0060.00614.89
7.3.50.0030.00714.98
7.3.40.0040.00414.96
7.3.30.0000.01514.58
7.3.20.0000.00916.48
7.3.10.0050.00716.51
7.3.00.0090.00516.55
7.2.250.0070.01115.06
7.2.240.0110.01115.26
7.2.230.0100.00615.23
7.2.220.0060.00314.91
7.2.210.0090.00614.82
7.2.200.0060.00915.30
7.2.190.0100.00315.12
7.2.180.0040.01115.15
7.2.170.0000.01614.79
7.2.130.0060.00616.36
7.2.120.0040.00816.42
7.2.110.0040.00816.78
7.2.100.0080.00416.56
7.2.90.0040.00816.83
7.2.80.0040.00817.03
7.2.70.0060.00616.56
7.2.60.0070.00616.78
7.2.50.0040.00816.60
7.2.40.0070.01116.86
7.2.30.0080.00816.92
7.2.20.0030.01218.10
7.2.10.0090.00618.28
7.2.00.0070.01118.20
7.1.330.0040.01115.66
7.1.320.0100.00715.74
7.1.310.0040.01115.68
7.1.300.0030.00915.56
7.1.290.0030.01215.68
7.1.280.0040.01115.77
7.1.270.0000.00915.69
7.1.260.0030.01115.78
7.1.250.0040.00815.58
7.1.200.0060.00615.30
7.1.140.0070.01518.64
7.1.130.0110.01318.64
7.1.120.0070.01118.68
7.1.110.0090.00718.09
7.1.100.0060.01317.75
7.1.90.0090.01217.94
7.1.80.0050.01617.93
7.1.70.0080.01217.16
7.1.60.0300.01334.96
7.1.50.0250.01134.85
7.1.40.0210.01534.05
7.1.30.0260.00934.48
7.1.20.0180.01634.63
7.1.10.0080.00816.36
7.1.00.0070.01016.67

preferences:
31.64 ms | 400 KiB | 5 Q