Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created September 22, 2015 12:41
Show Gist options
  • Save tommcfarlin/a10a4d27b6990ecd9640 to your computer and use it in GitHub Desktop.
Save tommcfarlin/a10a4d27b6990ecd9640 to your computer and use it in GitHub Desktop.
[PHP] Refactoring a script to use functions in order to make it a little more maintainable over the course of time.
<?php
if ( empty( $_GET['data'] ) ) {
echo "-1";
die;
}
$query_string = '';
$url = '' . '?' . $query_string;
$result = file_get_contents( $url );
echo ( 'request completed successfully' == $result ) ? "success" : "failure";
die;
<?php
/**
* @param string $url The URL to which we're making the request.
* @return string $output The result of the request.
*/
function get_request_response ( $url ) {
/* If cURL is installed, then use it to make the request with the specified URL.
* If not, then fallback to file_get_contents and return the response.
*/
return $output
}
/**
* @return bool $is_valid True if the data is valid; otherwise, false.
*/
function data_is_valid() {
$is_valid = true;
// Check to see if arguments in the $_GET collection are set
return $is_valid;
}
if ( data_is_valid() ) {
// 1. Define the query string parameters here.
$query_string = '';
// 2. Define the URL to which you're going to be making the request.
$url = '';
// 3. Add the query string parameters to the URL
$url .= '?' . $query_string;
// 4. Now get the result and echo it to the client
$result = get_request_response( $url );
echo ( 'request completed successfully' == $result ) ? "success" : "failure";
die;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment