PHP code example of pretorien / request

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

    

pretorien / request example snippets




return [
    // ...
    Pretorien\RequestBundle\RequestBundle::class => ['all' => true],
];


// src/Entity/Proxy.php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Pretorien\RequestBundle\Entity\Proxy as BaseProxy;

/**
 * @ORM\Entity
 * @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
 */
class Proxy extends BaseProxy
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

    use Pretorien\RequestBundle\Service\RequestService;

    /* ... */

    public function myFunction(RequestService $requestService)
    {
        /* ... */
    }

    use Pretorien\RequestBundle\Service\RequestService;
    use Pretorien\RequestBundle\Http\Request\Request;

    /* ... */

    public function myFunction(RequestService $requestService)
    {
        $url = "http://www.example.com";
        $options = [];
        $response = $requestService->request($url, Request::METHOD_GET, Request::TYPE_PUBLIC, $options);
        $response = $requestService->request($url, Request::METHOD_POST, Request::TYPE_PUBLIC, $options);
        $response = $requestService->request($url, Request::METHOD_GET, Request::TYPE_PRIVATE, $options);
        $response = $requestService->request($url, Request::METHOD_POST, Request::TYPE_PRIVATE, $options);
        /* ... */
    }

    use Pretorien\RequestBundle\Service\RequestService;
    use Pretorien\RequestBundle\Http\Request\Request;

    /* ... */

    public function myFunction(RequestService $requestService)
    {
        $url = "http://www.example.com";
        $options = [];
        $response = $requestService->publicRequest($url, Request::METHOD_GET, $options);
        $response = $requestService->privateRequest($url, Request::METHOD_GET, $options);
        /* ... */
    }

    use Pretorien\RequestBundle\Service\RequestService;

    /* ... */

    public function myFunction(RequestService $requestService)
    {
        $url = "http://www.example.com";
        $response = $requestService->privateRequest($url, [$method, $options]);
        // getting the response headers waits until they arrive
        $contentType = $response->getHeaders()['content-type'][0];

        // trying to get the response contents will block the execution until
        // the full response contents are received
        $contents = $response->getContent();
    }

    use Pretorien\RequestBundle\Service\RequestService;
    use Pretorien\RequestBundle\Http\Response\PoolResponse;
    use Pretorien\RequestBundle\Http\Request\PrivateRequest;
    use Pretorien\RequestBundle\Http\Request\PublicRequest;

    /* ... */

    public function myFunction(RequestService $requestService)
    {
        $url = "http://www.example.com";
        $pool = $requestService->createPoolRequest();
        $privateRequest = new PrivateRequest($url);
        $publicRequest = new PublicRequest($url);

        $pool->addRequest($privateRequest);
        $pool->addRequest($publicRequest);

        $poolResponse = $requestService->sendPoolRequest($pool);
    }

    use Pretorien\RequestBundle\Service\RequestService;
    use Pretorien\RequestBundle\Http\Response\PoolResponse;
    use Pretorien\RequestBundle\Http\Request\PrivateRequest;
    use Pretorien\RequestBundle\Http\Request\PublicRequest;

    /* ... */

    public function myFunction(RequestService $requestService)
    {
        $url = "http://www.example.com";
        $pool = $requestService->createPoolRequest();
        $privateRequest = new PrivateRequest($url);
        $publicRequest = new PublicRequest($url);

        $pool->addRequest($privateRequest);
        $pool->addRequest($publicRequest);

        $poolResponse = $requestService->sendPoolRequest($pool);
        $responses = $poolResponse->getContents();

        foreach ($responses[PoolResponse::RESPONSES_SUCCESSFUL] as $response) {
            $content = $response['content'];
            $request = $response['request'];
            $httpClientResponse = $response['response'];
        }

        foreach ($responses[PoolResponse::RESPONSES_FAILED] as $response) {
            $exception = $response['exception'];
            $request = $response['request'];
            $httpClientResponse = $response['response'];
        }
    }