PHP code example of rovazh / phpsocks

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

    

rovazh / phpsocks example snippets


$client = new \PhpSocks\Client([
    'host' => '127.0.0.1', // SOCKS5 server (IPv4, IPv6, or hostname)
    'port' => 1080, // SOCKS5 server port
]);

try {
    $stream = $client->connect('tcp://example.com:80');
    $stream->write("GET / HTTP/1.0\r\n\r\n");
    echo $stream->read(1024);
    $stream->close();
} catch (\PhpSocks\Exception\PhpSocksException $e) {
    // Handle exception
}

$client = new \PhpSocks\Client([
    'host' => '127.0.0.1',
    'port' => 1080,
]);

try {
    $stream = $client->connect('tls://example.net:443');
    $stream->write("GET / HTTP/1.0\r\n\r\n");
    echo $stream->read(1024);
    $stream->close();
} catch (\PhpSocks\Exception\PhpSocksException $e) {
    // Handle exception
}

$client = new \PhpSocks\Client([
    'host' => '127.0.0.1',
    'port' => 1080,
]);

try {
    $stream = $client->connect('tls://example.net:443', [
        'tls' => [
            'verify_peer' => false,
        ]
    ]);
    $stream->write("GET / HTTP/1.0\r\n\r\n");
    echo $stream->read(1024);
    $stream->close();
} catch (\PhpSocks\Exception\PhpSocksException $e) {
    // Handle exception
}

$client = new \PhpSocks\Client([
    'host' => '127.0.0.1',
    'port' => 1080,
    'auth' => [
        'username' => 'proxy_user',
        'password' => 'proxy_pass',
    ]
]);

$client = new \PhpSocks\Client([
    'host' => '127.0.0.1',
    'port' => 1080,
    'connect_timeout' => 5.0, // 5 seconds
]);

$client = new \PhpSocks\Client([
    'host' => '127.0.0.1',
    'port' => 1080,
    'connect_timeout' => 5.0, // 5 seconds
    'timeout' => 3, // 3 seconds
]);

$client = new \PhpSocks\Client([
    'host' => '127.0.0.1', // SOCKS5 server (IPv4, IPv6, or hostname)
    'port' => 1080, // SOCKS5 server port
]);

try {
    $stream = $client->associate('udp://example.com:5023');
    $stream->write("Hello");
    echo $stream->read(1024);
    $stream->close();
} catch (\PhpSocks\Exception\PhpSocksException $e) {
    // Handle exception
}