-
-
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.
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 | |
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; |
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 | |
/** | |
* @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