PHP code example of weew / http

1. Go to this page and download the library: Download weew/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/ */

    

weew / http example snippets


$response = new HttpResponse();
$response->send();

$response = new HttpResponse();
$response->setContent('<h1>Hello World!</h1>');
$response->send();

$response = new HttpResponse(HttpStatusCode::UNAUTHORIZED);
// or
$response = new HttpResponse(401);
$response->send();

$response = new HttpResponse();
$response->getHeaders()->set('foo', 'bar');
$response->send();

$response = new HttpResponse();
$response->getQueuedCookies()->add(new Cookie('foo', 'bar'));
$response->send();

$response = new HtmlResponse();
$response->setHtmlContent('<h1>Hello World!</h1>');
$response->send();

$response = new JsonResponse();
$response->getData()->set('Hello', 'World!');
$response->send();

$response = new BasicAuthResponse('Please login');
$response->send();

$request = new HttpRequest(
    HttpRequestMethod::POST,
    new Url('http://example.com')
);
$request->setContent('foo=bar');

$request = new HttpRequest();
$request->getUrl()->getQuery()->set('foo', 'bar');

echo $request->getUrl()->getQuery();
// foo=bar

$request = new HttpRequest();
$request->getData()->set('foo', 'bar');
$request->getData()->set('bar', 'foo');

echo $request->getContent();
// foo=bar&bar=foo

$request = new HttpRequest();
$request->getHeaders()->set('foo', 'bar');
$request->getHeaders()->add('foo', 'foo');

var_dump($request->getHeaders()->get('foo'));
// ['bar', 'foo']
echo $request->getHeaders()->find('foo');
// foo

$request = new CurrentRequest();
var_dump($request->toArray());
// all the data that the server received from the client

$request = new HttpRequest();
$request->getBasicAuth()->setUsername('foo');
$request->getBasicAuth()->setPassword('bar');
echo $request->getBasicAuth()->getToken();
// Zm9vOmJhcg==
echo $request->getHeaders()->find('authentication');
// Basic Zm9vOmJhcg==