Created
September 10, 2011 01:12
-
-
Save ram-nadella/1207776 to your computer and use it in GitHub Desktop.
For testing CloudFlare app integration
This file contains 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 | |
// Code Sample for the CloudFlare App API | |
// Copyright (c) 2011 by CloudFlare, Inc. | |
// All Rights Reserved. | |
// | |
// You may use these Code Samples for any purpose, commercial or otherwise, provided that you | |
// adhere to the disclaimer at the bottom of this document. You must also require any transferee of this | |
// Code Sample to abide by the terms of the disclaimer. | |
// | |
// Requires: PHP 5.0.3+ compiled with OpenSSL and CLI support | |
// Run from the command line like so: php test_app_api.php <ACTION> <params> | |
// | |
// Currently supports these API calls: | |
// | |
// * MYAPP_USER_CREATE | |
// | |
// Debug settings: | |
define('DEBUG_LEVEL', 1); // 0 for no debug, 1 for debug, 2 for more debug, etc. | |
define('PRINT_RESPONSE', 1); // 1 to print the received response | |
define('CF_MYAPP_KEY', 'blablabla'); | |
$act = $_SERVER["argv"][1]; | |
$responseHandler = ""; | |
$params = array(); | |
if (!$act) { | |
echo "ERROR: No action specified. (Actions: MYAPP_USER_CREATE)\n"; | |
echo "Usage: <action> [params]\n"; | |
exit(1); | |
} | |
switch (strtoupper($act)) { | |
// Create an account for a user. | |
case "MYAPP_USER_CREATE": | |
// Service URL: | |
define('GW_SERVICE_URL', 'http://192.168.1.101:8002/echo.html'); | |
// Handler | |
$responseHandler = "myapp_responseHandlerUserCreate"; | |
// required: | |
$params["act"] = "user_create"; | |
$params["api_key"] = CF_MYAPP_KEY; | |
$params["user_email"] = $_SERVER["argv"][2]; | |
$params["user_zone"] = $_SERVER["argv"][3]; | |
$user_tokens["fname"] = $_SERVER["argv"][4]; | |
$user_tokens["lname"] = $_SERVER["argv"][5]; | |
$params["user_tos"] = TRUE; | |
$params["user_tokens"] = json_encode($user_tokens); | |
// optional: | |
break; | |
default: | |
echo "That operation is not supported.\n"; | |
exit(4); | |
} | |
// Contact the service. Returns FALSE on error. | |
function performRequest(& $data, $headers=NULL) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, GW_SERVICE_URL); | |
if (DEBUG_LEVEL > 1) { | |
curl_setopt($ch, CURLOPT_VERBOSE, 1); | |
} | |
if (DEBUG_LEVEL > 0) { | |
echo "REQUEST DATA (to be sent via POST):\n"; | |
print_r($data); | |
} | |
if ($headers) { | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
} | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
if ($data) { | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
} | |
curl_setopt($ch, CURLOPT_TIMEOUT, 20); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); | |
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
if (($http_result = curl_exec($ch)) === FALSE) { | |
echo "WARNING: A connectivity error occured while contacting the service.\n"; | |
trigger_error(curl_error($ch)); | |
return FALSE; | |
} | |
curl_close($ch); | |
return $http_result; | |
} | |
function myapp_responseHandlerUserCreate(& $response) { | |
$params["user_email"] = $_SERVER["argv"][2]; | |
$params["user_zone"] = $_SERVER["argv"][3]; | |
$params["password"] = $_SERVER["argv"][4]; | |
if (PRINT_RESPONSE) { | |
echo "RAW RESPONSE DATA:\n".$response."\n"; | |
} | |
echo "DECODED RESPONSE DATA:\n"; | |
$decoded_response = json_decode($response, TRUE); | |
print_r($decoded_response); | |
assert('$decoded_response["request"]["act"] == "user_create"'); | |
assert('$decoded_response["response"]["user_email"] == $params["user_email"]'); | |
assert('$decoded_response["response"]["user_zone"] == $params["user_zone"]'); | |
assert('$decoded_response["result"] == "success" || $decoded_response["result"] == "failure"'); | |
assert('$decoded_response["msg"]'); | |
} | |
$response = performRequest($params); | |
if ($response === FALSE) { | |
die("Failed to get response from service."); | |
} | |
$responseHandler($response); | |
// | |
// Use of this Code Sample is at Your Own Risk | |
// | |
// You understand that your use of the this Code Sample is at your own risk, i.e. CloudFlare, Inc. | |
// (CloudFlare) will not be liable either directly or indirectly to you or any third party transferee recipient of the Code Sample. | |
// CloudFlare is under no obligation to provide you with any error corrections, updates, upgrades, bug fixes and/or enhancements of the Code Sample. | |
// | |
// This Code Sample May Not Include Adequate System Protection Functionality | |
// | |
// The code sample included herein has not been reviewed for suitability in terms of error-handling, system loading, memory requirements, | |
// CPU usage or other factors that may influence the operation of systems on which it is installed. | |
// | |
// Disclaimer of Warranty; Limitation of Liability | |
// | |
// YOUR USE OF THIS CODE SAMPLE IS AT YOUR SOLE RISK. ANY CODE SAMPLE IS PROVIDED "AS IS," "WITH ALL FAULTS" AND "AS AVAILABLE" FOR YOUR USE, | |
// WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, UNLESS SUCH WARRANTIES ARE LEGALLY INCAPABLE OF EXCLUSION. SPECIFICALLY, | |
// CloudFlare AND ITS VENDORS DISCLAIM IMPLIED WARRANTIES THAT THE CODE SAMPLE IS MERCHANTABLE, OF SATISFACTORY QUALITY, ACCURATE, FIT FOR A PARTICULAR | |
// PURPOSE OR NEED, OR NON-INFRINGING. CloudFlare AND ITS VENDORS DO NOT WARRANT THAT THE FUNCTIONS CONTAINED IN THE CODE SAMPLE WILL MEET YOUR REQUIREMENTS | |
// OR THAT THE CODE SAMPLE IS ERROR-FREE, OR THAT DEFECTS IN THE CODE SAMPLE WILL BE CORRECTED. CloudFlare AND ITS VENDORS DO NOT WARRANT OR MAKE ANY | |
// REPRESENTATIONS REGARDING THE USE OR THE RESULTS OF THE USE OF THE CODE SAMPLE OR RELATED DOCUMENTATION IN TERMS OF THEIR CORRECTNESS, ACCURACY, | |
// RELIABILITY OR OTHERWISE. | |
// | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment