Skip to content

Instantly share code, notes, and snippets.

@tobiastom
Forked from spacemuffin/phpvalidate.php
Created September 11, 2012 14:25
Show Gist options
  • Save tobiastom/3699033 to your computer and use it in GitHub Desktop.
Save tobiastom/3699033 to your computer and use it in GitHub Desktop.
Coupon validate methods
<?php
namespace redeemFunctions;
class ValidateRedeem {
/*
GetWebContent (Verwendet CURL PHP modul)
- ruft url auf -> JSON Rückgabe (JSON Decoder!)
- 1. Param = URL
- 2. Parm = Referenz fuer Statuscode
*/
public function getWebContent( $url, &$status ){
$jsonRetVal = '';
//use curl libs to get json object -> fill into $jsonRetVal
$ch = curl_init();
//set url
curl_setopt( $ch, CURLOPT_URL, $url );
//return the transfer as a string
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
//$output contains the output string
$output = curl_exec( $ch );
//convert otput as json
$jsonRetVal = json_decode( $output );
//close curl resource to free up system resources
curl_close( $ch );
//set statusReference by extracting status from json object
if ( isset( $jsonRetVal->redeemUrl ) ) {
$status = true;
} else {
$status = false;
}
return $jsonRetVal;
}
/*
VaildateCoupons (Soll GetWebContent nutzen!)
- Nimmt String entgegen mit Coupons (fb1123, ggs123456, 12346568, t5q6aw2v)
- Ruft Kiepenkerl UrL auf -> validate
- Rueckgabe 2 Dim Array -> Pos "InvalidCodes" als Array mit Codes, Pos "ValidCodes" als assoc Array-> Key CouponCode mit Praefix, Value Redeem URL.
*/
public function validateCoupons( $coupons ){
$invalidCodes = Array();
$validCodes = Array();
$validCouponChars = 'abcdezghijklmnopqrstuvwxyz1234567890';
$couponsLength = strlen( $coupons );
$foundCoupons = array();
$stack = '';
for( $x = 0; $x < $couponsLength; $x++ ) {
$char = $coupons[ $x ];
if ( false === strpos( $validCouponChars, strtolower( $char ) ) ) {
if ( $stack ) {
$foundCoupons[] = $stack;
}
$stack = null;
} else {
$stack .= $char;
}
}
$requestedCode = strtok( $coupons, ',');
foreach( $foundCoupons AS $coupon ) {
$status = true; //true = valid, false = invalid
$url = 'http://renes-workstation.local/~r.veldink/Kiepenkerl2/public/coupon;validate?coupon=' . trim( $coupon );
$jsonReturn = $this->getWebContent( $url , $status);
if ( !$status ) {
$invalidCodes[] = trim( $requestedCode ) ;
continue;
}
$validCodes[ trim( $requestedCode ) ] = isset( $jsonReturn->redeemUrl ) ? $jsonReturn->redeemUrl : null;
}
return Array( 'invalidCodes' => $invalidCodes, 'validCodes' => $validCodes );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment