User Tools

Site Tools


programming:php

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
programming:php [2021/04/14 02:07]
nanodano
programming:php [2022/03/13 19:00] (current)
nanodano ↷ Links adapted because of a move operation
Line 17: Line 17:
 </code> </code>
  
-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 215: Line 215:
 </code> </code>
  
-==== TCP server ====+ 
 +==== JSON ==== 
 + 
 +<code php json.php> 
 +<?php 
 +# PHP object -> JSON string 
 +$json_string = json_encode(['my_key' => 'my value', 'other_key' => 'other value']); 
 +echo $json_string; 
 + 
 +# JSON string -> PHP object 
 +$json_string = '{"my_key":"my value","other_key":"other value"}'; 
 +$my_array =  json_decode($json_string); 
 +print_r($my_array) 
 + 
 +// Dump/read from file 
 +file_put_contents('test.json', json_encode([2, 4, 6])); 
 +$my_array = json_decode(file_get_contents('test.json')); 
 +print_r($my_array); 
 +</code> 
 + 
 +==== Sockets ==== 
 + 
 +=== TCP server ===
  
 <code php tcp_server.php> <code php tcp_server.php>
Line 236: Line 258:
 </code> </code>
  
-==== Generate SSL certificate ====+=== Generate SSL certificate ===
  
 <code php make_ssl_cert.php> <code php make_ssl_cert.php>
Line 267: Line 289:
 </code> </code>
  
-==== SSL server ====+=== SSL server ===
  
 <code php ssl_server.php> <code php ssl_server.php>
Line 293: Line 315:
 </code> </code>
  
-==== SSL client ====+=== SSL client ===
  
 <code php ssl_client_simple.php> <code php ssl_client_simple.php>
Line 326: Line 348:
 } }
 </code> </code>
 +
 +==== 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("tail -f /path/to/file 2>&1", 'r');
 +while(!feof($handle)) {
 +    $buffer = fgets($handle);
 +    echo "$buffer<br/>\n";
 +    ob_flush();
 +    flush();
 +}
 +pclose($handle);
 +</code>
 +
 +==== Simple HTTP request ====
 +
 +<code php http_get.php>
 +<?php
 +$data = file_get_contents("https://www.devdungeon.com/");
 +print($data);
 +</code>
 +
 +==== cURL requests ====
 +
 +Install [[https://packages.debian.org/buster/php-curl|php-curl]] on Debian. [[https://curl.se|cURL]] lets you make all kinds of requests, not just HTTP. See my tutorial: [[https://www.devdungeon.com/content/curl-tutorial|cURL Tutorial]].
 +
 +<code php curl_example.php>
 +<?php
 +// apt install php-curl
 +$curlHandle = curl_init("http://www.devdungeon.com/");
 +$filePointer = fopen("output.txt", "w");
 +
 +// Set options before executing
 +curl_setopt($curlHandle, CURLOPT_FILE, $filePointer); // Output to file instead of STDOUT
 +curl_setopt($curlHandle, CURLOPT_HEADER, 1); // Include headers
 +curl_setopt($curlHandle, CURLOPT_VERBOSE, 1); // Output extra details
 +
 +// 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 "user-agent" header to be used in a HTTP request.
 +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 "POST" operation.
 +CURLOPT_COOKIE
 +CURLOPT_COOKIEFILE
 +CURLOPT_USERPWD // A string formatted in the username:password manner, for Curl to give to the remote server if requested.
 +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 "referer" header to be used in an HTTP request. This is only necessary if the remote server relies on this value.
 +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.
 +*/
 +</code>
 +
 +
  
programming/php.1618366048.txt.gz · Last modified: 2021/04/14 02:07 by nanodano