pushkar / php-useful
Contains various classes and functions to many common tasks in PHP.
v3.0.1
2019-11-07 10:58 UTC
Requires
- php: >=7.3
- ext-curl: *
- ext-mysqli: *
Requires (Dev)
- phpunit/phpunit: ^7 || ^8
README
A library of all commonly used PHP things.
Functions.php: has some most used functions
EasyHeaders.php: has methods to easily set headers
Curl.php: a wrapper for PHP ext-curl.
Installation
Use composer
composer require pushkar/php-useful
Or download the source and include mannualy
Usage
When using composer require the autoload file at the start of the php script
require_once __DIR__ . '/path/to/vendor/autoload.php';
Add the namespace
use PhpUseful\Functions; // for using the Functions class use PhpUseful\EasyHeaders; // for using the EasyHeader class use PhpUseful\Curl; // for using the Curl class;
All the methods are static except in the Curl class so you can directly use them without creating the class object.
Functions class
Example usage
$random_ str = Functions::randomString(int $length);
All the class methods are listed below.
static function randomString(int $length): string // returns a random string of given length static function escapeInput(string $input): string // returns escaped string for the given input string
EasyHeaders
For using methods from Easy class
EasyHeaders::not_found();
All the class methods are listed below.
/** * Sends a 301 MOVED PERMANENTLY header. */ static function moved_permanently() /** * Sends a 302 FOUND header. */ static function found() /** * Sends a 400 BAD REQUEST header. */ static function bad_request() /** * Sends a 401 UNAUTHORIZED header. */ static function unauthorized() /** * Sends a 403 FORBIDDEN header. */ static function forbidden() /** * Sends a 404 NOT FOUND header. */ static function not_found() /** * Sends a 408 REQUEST TIMEOUT header. */ static function request_timeout() /** * Sends a 500 INTERNAL SERVER ERROR header. */ static function server_error() /** * Sends a 501 NOT IMPLEMENTED header. */ static function not_implemented() /** * Sends a 502 BAD GATEWAY header. */ static function bad_gateway() /** * Sends a 503 SERVICE UNAVAULABLE header. */ static function service_unavailable() /** * Sends a 504 GATEWAY TIMEOUT header. */ static function gateway_timeout() /** * Sends a 422 Unprocessable Entity header. */ static function unprocessable_entity() /** * Sends a Content-Type: application/json header. */ static function json_header() /** * Sends a Content-Type: application/pdf header. */ static function pdf_header() /** * Sends a Content-Type: application/xml header. */ static function xml_header() /** * Sends a Content-Type: application/force-download header. */ static function download_header() /** * Sends a Content-Type: text-html header. */ static function html_header() /** * Sends a Content-Type: text/css header. */ static function css_header() /** * Sends a Content-Type: text/javascript header. */ static function js_header() /** * Sends a Content-Type: text/plain header. */ static function text_header()
Curl class
Example usage
require_once __DIR__ . '/path/to/vendor/autoload.php'; use PhpUseful\Curl; $curl = new Curl(); $curl->setUrl('http://example.com'); $curl->execute();
All methods
//Required. sets the url for the curl request. Throws Exception on invalid url. function setUrl(string $url) // Set headers for the curl request function setHeaders(array $headers) // when set to true it return the result of the curl_exec() as a string instead of outputting it directly. function setReturn(bool $bool) // when set to true POST method is used for the curl request function usePost(bool $bool) // set data to be sent via POST. For this to work usePost() should be set to true function setArrayPostData(array $data) // set data to be sent via POST. For this to work usePost() should be set to true function setStringPostData(string $data) // set the port number which will be used for the request function setPort(int $port) // set the max number of redirects that this request will follow function setMaxRedirects(int $max) // set the timeout for this request (in seconds) function setTimeout(int $timeout) // set the username and password to be used for HTTP authentication function setCredentials(string $username, string $password) // set any valid CURL option in case a helper method isn't present function setOption(int $option, mixed $value) // Call this after setting all url, headers, method & data function execute() // return the headers sent for the curl request. function getSentHeader() // return the reponse. Will work only when setReturn() is set to true. function getResult()