1. Go to this page and download the library: Download maplephp/http 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/ */
maplephp / http example snippets
use MaplePHP\Http\ServerRequest;
use MaplePHP\Http\Uri;
use MaplePHP\Http\Environment;
// Create an environment instance (wraps $_SERVER)
$env = new Environment();
// Create a URI instance from the environment
$uri = new Uri($env->getUriParts());
// Create the server request
$request = new ServerRequest($uri, $env);
// Get the HTTP method
$method = $request->getMethod(); // e.g., GET, POST
// Get request headers
$headers = $request->getHeaders();
// Get a specific header
$userAgent = $request->getHeaderLine('User-Agent');
// Get query parameters
$queryParams = $request->getQueryParams();
// Get parsed body (for POST requests)
$parsedBody = $request->getParsedBody();
// Get uploaded files
$uploadedFiles = $request->getUploadedFiles();
// Get server attributes
$attributes = $request->getAttributes();
// Add a new header
$newRequest = $request->withHeader('X-Custom-Header', 'MyValue');
// Change the request method
$newRequest = $request->withMethod('POST');
// Add an attribute
$newRequest = $request->withAttribute('user_id', 123);
use MaplePHP\Http\Response;
use MaplePHP\Http\Stream;
// Create a stream for the response body
$body = new Stream('php://temp', 'rw');
// Write content to the body
$body->write('Hello, world!');
$body->rewind();
// Create the response with the body
$response = new Response($body);
// Set the status code to 200 OK
$response = $response->withStatus(200);
// Add headers
$response = $response->withHeader('Content-Type', 'text/plain');
// Add multiple headers
$response = $response->withAddedHeader('X-Powered-By', 'MaplePHP');
// Output headers
foreach ($response->getHeaders() as $name => $values) {
foreach ($values as $value) {
header(sprintf('%s: %s', $name, $value), false);
}
}
// Output status line
header(sprintf(
'HTTP/%s %s %s',
$response->getProtocolVersion(),
$response->getStatusCode(),
$response->getReasonPhrase()
));
// Output body
echo $response->getBody();
use MaplePHP\Http\Stream;
// Create a stream from a file
//$fileStream = new Stream('/path/to/file.txt', 'r');
// Create a stream from a string
$memoryStream = new Stream(Stream::MEMORY);
//$memoryStream = new Stream('php://memory', 'r+'); // Same as above
$memoryStream->write('Stream content');
// Write to the stream
$memoryStream->write(' More content');
// Read from the stream
$memoryStream->rewind();
echo $memoryStream->getContents();
// Result: 'Stream content More content'
// Set stream as the body of a response
$response = $response->withBody($memoryStream);
// Create a URI instance
$uri = new Uri('http://example.com:8000/path?query=value#fragment');
// Modify the URI
$uri = $uri->withScheme('https')
->withUserInfo('guest', 'password123')
->withHost('example.org')
->withPort(8080)
->withPath('/new-path')
->withQuery('query=newvalue')
->withFragment('section1');
// Convert URI to string
echo $uri; // Outputs the full URI
//Result: https://guest:[email protected]:8080/new-path?query=newvalue#section1
// Get uploaded files from the request
$uploadedFiles = $request->getUploadedFiles();
// Access a specific uploaded file
$uploadedFile = $uploadedFiles['file_upload'];
// Get file details
$clientFilename = $uploadedFile->getClientFilename();
$clientMediaType = $uploadedFile->getClientMediaType();
// Move the uploaded file to a new location
$uploadedFile->moveTo('/path/to/uploads/' . $clientFilename);
use MaplePHP\Http\Client;
use MaplePHP\Http\Request;
// Init request client
$client = new Client([CURLOPT_HTTPAUTH => CURLAUTH_DIGEST]); // Pass on Curl options
// Create request data
$request = new Request(
"POST", // The HTTP Method (GET, POST, PUT, DELETE, PATCH)
"https://admin:[email protected]:443/test.php", // The Request URI
["customHeader" => "lorem"], // Add Headers, empty array is allowed
["email" => "[email protected]"] // Post data
);
// Pass request data to client and POST
$response = $client->sendRequest($request);
if ($response->getStatusCode() === 200) {
// Parse the response body
$data = json_decode($response->getBody()->getContents(), true);
// Use the data
echo 'User Name: ' . $data['name'];
} else {
echo 'Error: ' . $response->getReasonPhrase();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.