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 02:40] 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 213: | Line 213: | ||
$image-> | $image-> | ||
$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); | ||
</ | </ | ||
Line 328: | Line 348: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ==== 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. | ||
+ | */ | ||
+ | </ | ||
+ | |||
+ | |||