email smartphone

PHP Proxy Server einrichten

Wenn Sie PHP-Anwendungen auf einer IDNT Compute Cloud Instanz ausführen und auf das Internet zugreifen möchten, müssen Sie den IDNT Compute Cloud Proxy Server konfigurieren.

Stream Context für file_get_contents()

Für file_get_contents() können Sie einen Stream Context mit Proxy-Einstellungen verwenden:

<?php
$context = stream_context_create([
    'http' => [
        'proxy' => 'tcp://proxy.idnt.net:3128',
        'request_fulluri' => true,
    ],
    'https' => [
        'proxy' => 'tcp://proxy.idnt.net:3128',
        'request_fulluri' => true,
    ]
]);

$content = file_get_contents('https://example.com', false, $context);

cURL Proxy-Konfiguration

Für cURL-basierte HTTP-Requests:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com');
curl_setopt($ch, CURLOPT_PROXY, 'proxy.idnt.net:3128');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

Guzzle HTTP Client

Wenn Sie Guzzle verwenden:

<?php
use GuzzleHttp\Client;

$client = new Client([
    'proxy' => 'http://proxy.idnt.net:3128'
]);

$response = $client->get('https://example.com');

Symfony HTTP Client

Für Symfony HTTP Client:

<?php
use Symfony\Component\HttpClient\HttpClient;

$client = HttpClient::create([
    'proxy' => 'http://proxy.idnt.net:3128'
]);

$response = $client->request('GET', 'https://example.com');

Globale Proxy-Konfiguration mit Umgebungsvariablen

Sie können auch die Umgebungsvariablen setzen, die von vielen PHP-HTTP-Clients automatisch verwendet werden:

export http_proxy=http://proxy.idnt.net:3128
export https_proxy=http://proxy.idnt.net:3128
export HTTP_PROXY=http://proxy.idnt.net:3128
export HTTPS_PROXY=http://proxy.idnt.net:3128

Proxy-Server Details

  • Proxy-Server: proxy.idnt.net
  • Port: 3128
  • Protokoll: HTTP
  • Authentifizierung: Keine erforderlich

Testen der Konfiguration

Um die Proxy-Konfiguration zu testen, verwenden Sie:

<?php
$context = stream_context_create([
    'http' => [
        'proxy' => 'tcp://proxy.idnt.net:3128',
        'request_fulluri' => true,
    ]
]);

$content = file_get_contents('http://ifconfig.me', false, $context);
echo "Your IP: " . $content;

Weitere Informationen

Weitere Informationen zur Proxy-Konfiguration finden Sie in unseren verwandten Artikeln für Linux, WordPress und NodeJS.