This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
programming:php [2021/04/14 01:34] nanodano |
programming:php [2022/03/13 19:00] (current) nanodano ↷ Links adapted because of a move operation |
||
---|---|---|---|
Line 17: | Line 17: | ||
</ | </ | ||
- | See the [[:web_servers|web servers]] page for more info on configuring | + | See the [[:web|web servers]] page for more info on configuring |
===== Built-in webserver ===== | ===== Built-in webserver ===== | ||
Line 40: | Line 40: | ||
Phar files are essentially tarballs of PHP content. In addition to the convenience of having a single .phar containing many files, the phar can be marked executable and treated like an executable Java JAR. See the [[https:// | Phar files are essentially tarballs of PHP content. In addition to the convenience of having a single .phar containing many files, the phar can be marked executable and treated like an executable Java JAR. See the [[https:// | ||
- | It can be treated as a special protocol for referencing files like this: '' | + | It can be treated as a special protocol for referencing files like this: '' |
+ | You can read or include files in this manner. | ||
===== Composer ===== | ===== Composer ===== | ||
- | Refer to: [[https:// | + | Composer is an optional tool for managing depedencies. |
+ | |||
+ | See: [[https:// | ||
Line 61: | Line 64: | ||
</ | </ | ||
+ | ==== PHP info ==== | ||
+ | |||
+ | <code php php_info.php> | ||
+ | <?php | ||
+ | // Show all PHP configs/ | ||
+ | phpinfo(); | ||
+ | </ | ||
==== Show all errors ==== | ==== Show all errors ==== | ||
Line 164: | Line 174: | ||
$db = null; // Close it | $db = null; // Close it | ||
</ | </ | ||
+ | |||
+ | ==== libgit2 ==== | ||
+ | |||
+ | See: https:// | ||
+ | |||
+ | ==== Crop images ==== | ||
+ | |||
+ | <code php crop_image_gd.php> | ||
+ | // Crop using GD (`apt install php-gd`) | ||
+ | $src_img = imagecreatefrompng(' | ||
+ | if(!$src_img) { | ||
+ | die(' | ||
+ | } | ||
+ | $thumbnail = imagecreatetruecolor(200, | ||
+ | if(!$thumbnail) { | ||
+ | die(' | ||
+ | } | ||
+ | // Take 200x200 from 200x200 starting at 50,0 | ||
+ | $result = imagecopyresampled($thumbnail, | ||
+ | if(!$result) { | ||
+ | die(' | ||
+ | } | ||
+ | $result = imagejpeg($thumbnail, | ||
+ | if(!$result) { | ||
+ | die(' | ||
+ | } | ||
+ | $result = imagedestroy($thumbnail); | ||
+ | if(!$result) { | ||
+ | die(' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code php crop_image_imagick.php> | ||
+ | // Crop image using Imagick (`apt install php-imagick`) | ||
+ | $inFile = " | ||
+ | $outFile = " | ||
+ | $image = new Imagick($inFile); | ||
+ | $image-> | ||
+ | $image-> | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== JSON ==== | ||
+ | |||
+ | <code php json.php> | ||
+ | <?php | ||
+ | # PHP object -> JSON string | ||
+ | $json_string = json_encode([' | ||
+ | echo $json_string; | ||
+ | |||
+ | # JSON string -> PHP object | ||
+ | $json_string = ' | ||
+ | $my_array = json_decode($json_string); | ||
+ | print_r($my_array) | ||
+ | |||
+ | // Dump/read from file | ||
+ | file_put_contents(' | ||
+ | $my_array = json_decode(file_get_contents(' | ||
+ | print_r($my_array); | ||
+ | </ | ||
+ | |||
+ | ==== Sockets ==== | ||
+ | |||
+ | === TCP server === | ||
+ | |||
+ | <code php tcp_server.php> | ||
+ | <?php | ||
+ | $socket = stream_socket_server(" | ||
+ | if (!$socket) { | ||
+ | echo " | ||
+ | die(' | ||
+ | } | ||
+ | |||
+ | while (true) { | ||
+ | while ($conn = stream_socket_accept($socket, | ||
+ | fwrite($conn, | ||
+ | echo " | ||
+ | fclose($conn); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | fclose($socket); | ||
+ | </ | ||
+ | |||
+ | === Generate SSL certificate === | ||
+ | |||
+ | <code php make_ssl_cert.php> | ||
+ | <?php | ||
+ | $certificateData = array( | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ); | ||
+ | |||
+ | // Generate certificate | ||
+ | $privateKey = openssl_pkey_new(); | ||
+ | $certificate = openssl_csr_new($certificateData, | ||
+ | $certificate = openssl_csr_sign($certificate, | ||
+ | |||
+ | // Generate PEM file | ||
+ | $pem_passphrase = ' | ||
+ | $pem = array(); | ||
+ | openssl_x509_export($certificate, | ||
+ | openssl_pkey_export($privateKey, | ||
+ | $pem = implode($pem); | ||
+ | |||
+ | // Save PEM file | ||
+ | $pemfile = ' | ||
+ | file_put_contents($pemfile, | ||
+ | </ | ||
+ | |||
+ | === SSL server === | ||
+ | |||
+ | <code php ssl_server.php> | ||
+ | <?php | ||
+ | $context = stream_context_create(); | ||
+ | |||
+ | // local_cert must be in PEM format | ||
+ | stream_context_set_option($context, | ||
+ | |||
+ | // Pass Phrase (password) of private key | ||
+ | stream_context_set_option($context, | ||
+ | stream_context_set_option($context, | ||
+ | stream_context_set_option($context, | ||
+ | |||
+ | // Create the server socket | ||
+ | $socket = stream_socket_server( | ||
+ | ' | ||
+ | $errno, | ||
+ | $errstr, | ||
+ | STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, | ||
+ | $context | ||
+ | ); | ||
+ | |||
+ | // fwrite/ | ||
+ | </ | ||
+ | |||
+ | === SSL client === | ||
+ | |||
+ | <code php ssl_client_simple.php> | ||
+ | <?php | ||
+ | // Simple SSL client | ||
+ | $socket = stream_socket_client(" | ||
+ | if ($socket) { echo fread($socket, | ||
+ | </ | ||
+ | |||
+ | <code php ssl_client_full.php> | ||
+ | <?php | ||
+ | $host = ' | ||
+ | $port = 5522; | ||
+ | $timeout = 30; | ||
+ | $cert = ' | ||
+ | $context = stream_context_create( | ||
+ | array(' | ||
+ | ); | ||
+ | if ($socket = stream_socket_client( | ||
+ | ' | ||
+ | $errno, | ||
+ | $errstr, | ||
+ | 30, | ||
+ | STREAM_CLIENT_CONNECT, | ||
+ | $context) | ||
+ | ) { | ||
+ | fwrite($socket, | ||
+ | echo fread($socket, | ||
+ | fclose($socket); | ||
+ | } else { | ||
+ | echo " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Tail a file ==== | ||
+ | |||
+ | <code php tail.php> | ||
+ | <?php | ||
+ | // The flushing may be affected by your web server and your browser | ||
+ | // and how much data they require before they will flush the buffer. | ||
+ | $handle = popen(" | ||
+ | while(!feof($handle)) { | ||
+ | $buffer = fgets($handle); | ||
+ | echo " | ||
+ | ob_flush(); | ||
+ | flush(); | ||
+ | } | ||
+ | pclose($handle); | ||
+ | </ | ||
+ | |||
+ | ==== Simple HTTP request ==== | ||
+ | |||
+ | <code php http_get.php> | ||
+ | <?php | ||
+ | $data = file_get_contents(" | ||
+ | print($data); | ||
+ | </ | ||
+ | |||
+ | ==== cURL requests ==== | ||
+ | |||
+ | Install [[https:// | ||
+ | |||
+ | <code php curl_example.php> | ||
+ | <?php | ||
+ | // apt install php-curl | ||
+ | $curlHandle = curl_init(" | ||
+ | $filePointer = fopen(" | ||
+ | |||
+ | // Set options before executing | ||
+ | curl_setopt($curlHandle, | ||
+ | curl_setopt($curlHandle, | ||
+ | curl_setopt($curlHandle, | ||
+ | |||
+ | // Execute and clean up | ||
+ | curl_exec($curlHandle); | ||
+ | curl_close($curlHandle); | ||
+ | fclose($filePointer); | ||
+ | |||
+ | // Other available options | ||
+ | /* | ||
+ | CURLOPT_VERBOSE // 1 if you want Curl to give detailed reports about everything that is happening. | ||
+ | CURLOPT_URL // String containing the URL you want Curl to fetch. | ||
+ | CURLOPT_USERAGENT // A string containing the " | ||
+ | CURLOPT_TIMEOUT // A number equal to the maximum time in seconds that Curl functions can take. | ||
+ | CURLOPT_NOBODY // 1 to tell Curl not to include the body part in the output. For HTTP(S) servers, this is equivalent to a HEAD request - only the headers will be returned. | ||
+ | CURLOPT_POST // 1 if you want Curl to do a regular HTTP POST. | ||
+ | CURLOPT_POSTFIELDS // A string containing the data to post in the HTTP " | ||
+ | CURLOPT_COOKIE | ||
+ | CURLOPT_COOKIEFILE | ||
+ | CURLOPT_USERPWD // A string formatted in the username: | ||
+ | CURLOPT_RESUME_FROM // A number equal to the offset, in bytes, that you want your transfer to start from. | ||
+ | CURLOPT_FOLLOWLOCATION // 1 to follow Location: headers | ||
+ | CURLOPT_MAXREDIRS // limits FollowLocations | ||
+ | CURLOPT_REFERER // A string containing the " | ||
+ | CURLOPT_FAILONERROR // 1 for silent fail | ||
+ | CURLOPT_FTPAPPEND // append not overwrite | ||
+ | CURLOPT_FTPLISTONLY // 1 to list just the names of an FTP directory as opposed to more detailed information. | ||
+ | CURLOPT_HTTPHEADER //An array of HTTP header fields to be set. | ||
+ | CURLOPT_INFILE // String containing the filename where the input of your transfer comes from. | ||
+ | CURLOPT_INFILESIZE //The size of the file being uploaded to a remote site. | ||
+ | CURLOPT_CRLF // convert unix to CRLF new lines | ||
+ | CURLOPT_RETURNTRANSFER // 1 if you want Curl to return the transfer data instead of printing it out directly. | ||
+ | CURLOPT_STDERR // A string containing the filename to write errors to instead of normal output. | ||
+ | CURLOPT_UPLOAD // 1 if you want PHP to prepare for a file upload. | ||
+ | CURLOPT_WRITEHEADER // containing the filename to write the header part of the output into. | ||
+ | */ | ||
+ | </ | ||
+ | |||
+ | |||
+ |