PHP code example of kajna / purli

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

    

kajna / purli example snippets


try {
    $purli = (new \Purli\Purli())
        ->get('http://www.example.com')
        ->close();

    $response = $purli->response();

    echo $response->asText();
} catch(\Exception $e) {
    echo $e->getMessage();
}

try {
    $purli = (new \Purli\Purli(\Purli\Purli::SOCKET))
            ->get('http://example.com')
            ->close();
    
    $response = $purli->response();
    
    echo $response->asText();
} catch(\Exception $e) {
	echo $e->getMessage();
}

try {
    $data = array('foo' => 'bar');

    $purli = (new \Purli\Purli())
        ->setParams($data)
        ->post('http://www.example.com')
        ->close();

    $response = $purli->response();

    print_r($response->asText());
} catch(\Exception $e) {
    echo $e->getMessage();
}

try {
    $data = '<root><foo>bar</foo></root>';

    $purli = (new \Purli\Purli())
        ->setUserAgent('curl 7.16.1 (i386-portbld-freebsd6.2) libcurl/7.16.1 OpenSSL/0.9.7m zlib/1.2.3')
        ->setHeader('Content-Type', 'text/xml')
        ->setParams($data)
        ->post('http://www.example.com')
        ->close();

    $response = $purli->response();

    print_r($response->asArray());
} catch(\Exception $e) {
    echo $e->getMessage();
}

try {
    $data = array('foo' => 'bar');
    $json = json_encode($data);
    
    $purli = (new \Purli\Purli(\Purli\Purli::SOCKET))
            ->setConnectionTimeout(3)
            ->setHeader('Content-Type', 'application/json')
            ->setParams($json)
            ->put('http://www.example.com')
            ->close();
    
    $response = $purli->response();
    
    print_r($response->asObject());
} catch(\Exception $e) {
	echo $e->getMessage();
}

try {
    $purli = (new \Purli\Purli());
    
    $purli
        ->setProxy(PROXY_ADDRESS, PROXY_PORT)
        ->get('http://www.example.com')
        ->close();

    $response = $purli->response();

    echo $response->asText();
} catch(\Exception $e) {
    echo $e->getMessage();
}

try {
    $purli = (new \Purli\Purli());
    
    if ($purli->getHandlerType() === \Purli\Purli::CURL) {
        curl_setopt($purli->getHandler(), CURLOPT_TIMEOUT, 10);
    }
    
    $purli
        ->get('http://www.example.com')
        ->close();

    $response = $purli->response();

    echo $response->asText();
} catch(\Exception $e) {
    echo $e->getMessage();
}