PHP code example of pushkar / php-useful

1. Go to this page and download the library: Download pushkar/php-useful library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pushkar / php-useful example snippets

 
 

 use PhpUseful\Functions;   // for using the Functions class
 use PhpUseful\EasyHeaders; // for using the EasyHeader class
 use PhpUseful\Curl;        // for using the Curl class;

 $random_ str = Functions::randomString(int $length);

    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::not_found();

/**
* 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()

     use PhpUseful\Curl;
    
    $curl = new Curl();
    $curl->setUrl('http://example.com');
    $curl->execute();

    //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()