1. Go to this page and download the library: Download mnavarrocarter/php-fetch 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/ */
mnavarrocarter / php-fetch example snippets
use function MNC\Http\fetch;
$response = fetch('https://mnavarro.dev');
// Emit the response to stdout
while (($chunk = $response->body()->read()) !== null) {
echo $chunk;
}
use function MNC\Http\fetch;
use Castor\Io\Eof;
$response = fetch('https://some-domain.example/some-form', [
'method' => 'POST',
'headers' => [
'Content-Type' => 'application/json',
'User-Agent' => 'PHP Fetch'
],
'body' => json_encode(['data' => 'value'])
]);
// Emit the response to stdout in chunks
while (true) {
$chunk = '';
try {
$response->body()->read(4096, $chunk);
} catch (Eof $e) {
break;
}
echo $chunk;
}
use function MNC\Http\fetch;
$response = fetch('https://mnavarro.dev');
echo $response->status()->protocolVersion(); // 1.1
echo $response->status()->code(); // 200
echo $response->status()->reasonPhrase(); // OK
echo $response->headers()->has('content-type'); // true
echo $response->headers()->contains('content-type', 'html'); // true
echo $response->headers()->get('content-type'); // text/html;charset=utf-8
$bytes = '';
echo $response->body()->read(4096, $bytes); // Allocates reader data into $bytes
echo $bytes; // Outputs some bytes from the response body
use function Castor\Io\readAll;
use function MNC\Http\fetch;
$response = fetch('https://mnavarro.dev');
echo readAll($response->body()); // Buffers all the contents in memory and emits them.
use MNC\Http\Encoding\Json;
use function MNC\Http\fetch;
$response = fetch('https://api.github.com/users/mnavarrocarter', [
'headers' => [
'User-Agent' => 'PHP Fetch 1.0' // Github api
use MNC\Http\StandardHeaders;
use function MNC\Http\fetch;
$response = fetch('https://mnavarro.dev');
$stdHeaders = StandardHeaders::from($response);
$lastModified = $stdHeaders->getLastModified()->diff(new DateTimeImmutable(), true)->h;
echo sprintf('This html content was last modified %s hours ago...', $lastModified) . PHP_EOL;