Created
November 20, 2014 16:20
-
-
Save krhoyt/894ee142630bb040aac3 to your computer and use it in GitHub Desktop.
Personal data proxy.
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
<html> | |
<head> | |
<title>Proxy Test</title> | |
<script> | |
// Echo | |
var echo = { | |
action: "ECHO", | |
whatever: { | |
any: "value", | |
you: "want" | |
} | |
}; | |
// HTTP GET | |
var http = { | |
action: "HTTP", | |
details: { | |
method: "GET", | |
url: "http://kevinhoyt.com/blog/feed.xml" | |
} | |
}; | |
// Parse object create | |
var parse = { | |
action: "PARSE", | |
details: { | |
app_id: "_APP_ID_", | |
data: { | |
celcius: 12.34, | |
fahrenheit: 56.78 | |
}, | |
data_type: "Temperature", | |
operation: "create", | |
rest_key: "_REST_KEY_", | |
unit: "object", | |
version: 1 | |
} | |
}; | |
var xhr = null; | |
xhr = new XMLHttpRequest(); | |
xhr.addEventListener( "load", doProxyLoad ); | |
xhr.open( "POST", "http://proxy.kevinhoyt.com" ); | |
xhr.send( JSON.stringify( echo ) ); | |
function doProxyLoad() | |
{ | |
console.log( xhr.responseText ); | |
xhr.removeEventListener( "load", doProxyLoad ); | |
xhr = null; | |
} | |
</script> | |
<body> | |
Proxy all teh codez! | |
</body> | |
</html> |
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 | |
// Constants | |
$ACTION_HTTP = "HTTP"; | |
$ACTION_ECHO = "ECHO"; | |
$ACTION_PARSE = "PARSE"; | |
$ACTION_PUBNUB = "PUBNUB"; | |
$ACTION_STOMP = "STOMP"; | |
$HTTP_GET = "GET"; | |
$HTTP_POST = "POST"; | |
$PARSE_CREATE = "create"; | |
$PARSE_OBJECT = "object"; | |
// Holder JSON data | |
$json = file_get_contents( "php://input" ); | |
if( strlen( $json ) == 0 ) | |
{ | |
$json = '{"action":"ECHO","details":{"error":"No commands provided.","info":"These are not the services you are looking for.","visit":"http://kevinhoyt.com"}}'; | |
} | |
// Decode incoming data | |
$options = json_decode( $json ); | |
// Echo | |
if( $options -> action == $ACTION_ECHO ) | |
{ | |
// Return same as incoming | |
$response = $json; | |
} | |
// HTTP | |
if( $options -> action == $ACTION_HTTP ) | |
{ | |
// Initialize cURL | |
$curl = curl_init(); | |
// Return content | |
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); | |
// GET | |
if( $options -> details -> method == $HTTP_GET ) | |
{ | |
curl_setopt( $curl, CURLOPT_URL, $options -> details -> url ); | |
} | |
// Make the request | |
$response = curl_exec( $curl ); | |
curl_close( $curl ); | |
} | |
// Parse.com | |
if( $options -> action == $ACTION_PARSE ) | |
{ | |
echo "Going to Parse.com\n"; | |
// Initialize cURL | |
$curl = curl_init(); | |
// Return content | |
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); | |
// CRUD | |
if( $options -> details -> unit == $PARSE_OBJECT ) | |
{ | |
// API URL | |
// SSL | |
// Authentication headers | |
curl_setopt( $curl, CURLOPT_URL, | |
"https://api.parse.com/" . | |
$options -> details -> version . | |
"/classes/" . | |
$options -> details -> data_type | |
); | |
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false ); | |
curl_setopt( $curl, CURLOPT_HTTPHEADER, array( | |
"X-Parse-Application-Id: " . $options -> details -> app_id, | |
"X-Parse-REST-API-Key: " . $options -> details -> rest_key, | |
"Content-Type: application/json" | |
) ); | |
// Create | |
if( $options -> details -> operation == $PARSE_CREATE ) | |
{ | |
// POST | |
// Record as JSON string | |
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $HTTP_POST ); | |
curl_setopt( $curl, CURLOPT_POSTFIELDS, json_encode( $options -> details -> data ) ); | |
} | |
} | |
// Make the request | |
$response = curl_exec( $curl ); | |
curl_close( $curl ); | |
} | |
// PubNub | |
if( $options -> action == $ACTION_PUBNUB ) | |
{ | |
} | |
// STOMP | |
if( $options -> action == $ACTION_STOMP ) | |
{ | |
} | |
// Send the results back | |
echo $response; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment