Created
September 6, 2012 11:51
-
-
Save spacemuffin/3655372 to your computer and use it in GitHub Desktop.
Coupon validate methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
$requestedCode = strtok( $coupons, ','); | |
while ($requestedCode !== false) { | |
$status = true; //true = valid, false = invalid | |
if($requestedCode){ | |
$url = 'http://renes-workstation.local/~r.veldink/Kiepenkerl2/public/coupon;validate?coupon=' . trim( $requestedCode ); | |
$jsonReturn = $this->getWebContent( $url , $status); | |
if ( $status ) { | |
if( isset( $jsonReturn->redeemUrl ) ) { | |
$validCodes[ trim( $requestedCode ) ] = $jsonReturn->redeemUrl; | |
} else { | |
$validCodes[ trim( $requestedCode ) ] = null; | |
} | |
} else { | |
//$invalidCodes[ trim( $requestedCode ) ] = null; | |
$invalidCodes[] = trim( $requestedCode ) ; | |
} | |
} | |
$requestedCode = strtok( ','); | |
} | |
return Array( 'invalidCodes' => $invalidCodes, 'validCodes' => $validCodes ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment