1. Go to this page and download the library: Download symplely/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/ */
symplely / http example snippets
use Async\Http\ServerRequest;
$request = new ServerRequest(...);
$newRequest = $request->withHeader(
'Content-Type',
'text/html'
);
$newRequest = $request->withHeader(
'Accept',
['application/json', 'application/xml']
);
use Async\Http\Request;
$request = Request::create('GET', '/some/path?foo=bar');
use Async\Http\Request;
$method = 'GET';
$uri = 'http://example.com/';
$headers = ['Content-Type' => 'application/json'];
$body = '{"ping": "pong"}';
$protocolVersion = '1.1';
// All of the parameters are optional.
$request = new Request(
$method,
$uri,
$headers,
$body,
$protocolVersion
);
use Async\Http\ServerRequestFactory;
use Psr\Http\Message\ServerRequestInterface;
$factory = new ServerRequestFactory();
/** @var ServerRequestInterface */
$request = $factory->createServerRequest('GET', '/some/path?foo=bar');
$request = $factory->createServerRequest('GET', '/some/path?foo=bar', $serverParams);
use Async\Http\ServerRequest;
$request = new ServerRequest(...);
// If you have a route such as /product/{id}
// And a request for /product/123
// You can set the 'id' attribute to the product ID
$newRequest = $request->withAttribute('id', 123);
// Some controller for the route
$controller = function ($request) {
// Look up product data
$productId = $request->getAttribute('id');
$product = $someRepository->find($productId);
// Do something with $product
};
$controller($newRequest);
use Async\Http\Response;
// Defaults to a 200 OK response.
$response = new Response('Hello, world!');
// Use a given status code.
$response = new Response(204);
// Send custom headers.
$response = new Response(
302,
'Goodbye, world!',
['Location' => '/bye-bye']
);
use Async\Http\Response;
$response = new Response(...);
$newResponse = $response->withStatus(204);
$newResponse = $response->withStatus(204, 'No Content');
use Async\Http\JsonResponse;
// Defaults to a 200 OK response.
$response = new JsonResponse(['message' => 'Hello, world!']);
// Custom 404 response.
$response = new JsonResponse(
['error' => 'Page not found'],
404
);
// Include additional headers.
$response = new JsonResponse(
['error' => 'Invalid credentials'],
401,
['X-Auth' => 'Failed']
);
use Async\Http\RedirectResponse;
// Defaults to a 302 redirect.
$redirect = new RedirectResponse('/some/path');
// Use a given status code.
$redirect = new RedirectResponse('/some/path', 301);
// Send custom headers.
$redirect = new RedirectResponse(
'/some/path',
302,
['X-Message' => 'Bye-bye']
);
use Async\Http\Stream;
$stream = Stream::create('string of data');
$stream = Stream::createFromFile('/path/to/file', 'r');
$resource = fopen('/path/to/file', 'wb+');
$stream = Stream::createFromResource($resource);
use Async\Http\Stream;
$stream = new Stream('string of data');
$stream = new Stream($resource);
use Async\Http\Uri;
$uri = Uri::create('/some/path?foo=bar');
$uri = Uri::create('https://example.com/search?q=test');
use Async\Http\Uri;
$uri = new Uri('/some/path?foo=bar');
$uri = new Uri('https://example.com/search?q=test');
use Async\Http\Cookies;
use Async\Http\SetCookies;
// Get a collection representing the cookies in the `Cookie` headers
// of a PSR-7 Request.
$cookies = Cookies::fromRequest($request);
// Get a collection representing the cookies in the Set-Cookie headers
// of a PSR-7 Response
$setCookies = SetCookies::fromResponse($response);
// Put the `Cookie` headers and add them to the headers of a
// PSR-7 Request.
$request = $cookies->intoHeader($request);
// Put the `Set-Cookie` headers and add them to the headers of a
// PSR-7 Response.
$response = $setCookies->intoHeader($response);
use Async\Http\Cookie;
// Parse Set-Cookie header(s) and create an instance of CookieInterface.
$cookie = (new Cookie())
->create('PHPSESS=1234567890; Domain=domain.tld; Expires=Wed, 21 Oct 2015 07:28:00 GMT; HttpOnly; Max-Age=86400; Path=/admin; Secure');
// After making changes you can just cast it to a RFC-6265 valid string as show below.
$header = (string) $cookie;
use Async\Http\Cookie;
$cookie = Cookie::make('theme', 'blue');
use Async\Http\RequestCookies;
$request = RequestCookies::set($request, Cookie::make('theme', 'blue'));
use Async\Http\RequestCookies;
$modify = function (Cookie $cookie) {
$value = $cookie->getValue();
// ... inspect current $value and determine if $value should
// change or if it can stay the same. in all cases, a cookie
// should be returned from this callback...
return $cookie->withValue($value);
}
$request = RequestCookies::modify($request, 'theme', $modify);
use Async\Http\RequestCookies;
$request = RequestCookies::remove($request, 'theme');
use Async\Http\ResponseCookies;
$response = ResponseCookies::set($response, SetCookie::create('token')
->withValue('a9s87dfz978a9')
->withDomain('example.com')
->withPath('/firewall')
);
use Async\Http\ResponseCookies;
$modify = function (SetCookie $setCookie) {
$value = $setCookie->getValue();
// ... inspect current $value and determine if $value should
// change or if it can stay the same. in all cases, a cookie
// should be returned from this callback...
return $setCookie
->withValue($newValue)
->withExpires($newExpires)
;
}
$response = ResponseCookies::modify($response, 'theme', $modify);
use Async\Http\ResponseCookies;
$response = ResponseCookies::remove($response, 'theme');
use Async\Http\ResponseCookies;
$response = ResponseCookies::expire($response, 'session_cookie');
$session = new Sessions();
/**
* Session is started, populated with default parameters and the response has session cookie header.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param RequestHandlerInterface|callable|null $next
*
* The callable should have something similar to this signature:
* function (ServerRequestInterface $request, ResponseInterface $response) : Response {
* // your code
* }
*/
$response = $session($request, $response, $next);