Ir al contenido
Secciones
Documentación

PHP

Integra tu IPv4 estática dedicada en PHP con Guzzle, cURL nativo o el cliente Http de Laravel sin fugas DNS ni problemas con config:cache.

En esta página7

Como la mayoría de los clientes HTTP de PHP se apoyan en libcurl, los tres esquemas de proxy (https://, http:// y socks5h://) funcionan de forma uniforme en todo el ecosistema:

Herramienta Esquemas de proxy Ideal para
Guzzle https://, http://, socks5h:// Aplicaciones PHP generales y SDK
cURL nativo https://, http://, socks5h:// Scripts PHP sin dependencias
Laravel Http https://, http://, socks5h:// Aplicaciones Laravel (envuelve Guzzle)

Paso 1. Obtén tu URL de proxy

  1. Abre Direcciones IP en tu panel de Prontaro (my.prontaro.com/ips) y selecciona tu IPv4.
  2. Copia la URL HTTPS (:443) o SOCKS5 (:1080) en Credenciales del proxy.
Direcciones IPCredenciales de proxyAbrir en el panel

Exporta la cadena del proxy en PRONTARO_PROXY_URL:

bash
export PRONTARO_PROXY_URL="https://user:pass@<entry>.prontaro.io:443"

Guzzle (guzzlehttp/guzzle)

Instala guzzlehttp/guzzle y pasa PRONTARO_PROXY_URL en la opción proxy:

bash
composer require guzzlehttp/guzzle
php
<?php
// check_guzzle.php
require __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Client;

$proxyUrl = getenv('PRONTARO_PROXY_URL');
if ($proxyUrl === false || $proxyUrl === '') {
    throw new RuntimeException('PRONTARO_PROXY_URL is not set');
}

$client = new Client([
    'proxy' => $proxyUrl, // supports https://, http://, and socks5h://
    'connect_timeout' => 5.0,
    'timeout' => 15.0,
]);

$response = $client->get('https://api.ipify.org');
echo trim((string) $response->getBody()), PHP_EOL;

Comprueba tu IP de salida:

php check_guzzle.php
203.0.113.10

Guzzle lanza una excepción por defecto ante respuestas 4xx/5xx. Cuando api.ipify.org devuelva tu IPv4 dedicada, puedes restringir los destinos en el panel (Direcciones IP → Credenciales del proxy → Acceso).

cURL nativo (CURLOPT_PROXY y CURLPROXY_SOCKS5_HOSTNAME)

Para proxies https:// y http://, pasa la cadena completa del proxy a CURLOPT_PROXY:

php
<?php
// check_curl.php
$proxyUrl = getenv('PRONTARO_PROXY_URL');
if ($proxyUrl === false || $proxyUrl === '') {
    throw new RuntimeException('PRONTARO_PROXY_URL is not set');
}

$ch = curl_init('https://api.ipify.org');
curl_setopt_array($ch, [
    CURLOPT_PROXY => $proxyUrl,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_TIMEOUT => 15,
]);

$body = curl_exec($ch);
if ($body === false) {
    throw new RuntimeException('cURL error: ' . curl_error($ch));
}

$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($status < 200 || $status >= 300) {
    throw new RuntimeException("HTTP {$status}: {$body}");
}

echo trim($body), PHP_EOL;

Al configurar SOCKS5 explícitamente con CURLOPT_PROXYTYPE, usa siempre CURLPROXY_SOCKS5_HOSTNAME en lugar de CURLPROXY_SOCKS5:

php
<?php
// check_socks.php
// PRONTARO_SOCKS_URL="socks5h://user:pass@<entry>.prontaro.io:1080"
$parts = parse_url(getenv('PRONTARO_SOCKS_URL') ?: '');
if (!is_array($parts) || empty($parts['host']) || empty($parts['port'])) {
    throw new RuntimeException('Invalid PRONTARO_SOCKS_URL');
}

$ch = curl_init('https://api.ipify.org');
curl_setopt_array($ch, [
    CURLOPT_PROXY => "{$parts['host']}:{$parts['port']}",
    CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5_HOSTNAME, // resolves DNS on the Prontaro node
    CURLOPT_PROXYUSERPWD => rawurldecode($parts['user'] ?? '') . ':' . rawurldecode($parts['pass'] ?? ''),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_TIMEOUT => 15,
]);

$body = curl_exec($ch);
if ($body === false) {
    throw new RuntimeException('cURL error: ' . curl_error($ch));
}

$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($status < 200 || $status >= 300) {
    throw new RuntimeException("HTTP {$status}: {$body}");
}

echo trim($body), PHP_EOL;

Laravel Http::withOptions

Primero mapea la variable de entorno en config/services.php: llamar directamente a env() en el código devuelve null después de ejecutar php artisan config:cache:

php
// config/services.php
'prontaro' => [
    'proxy_url' => env('PRONTARO_PROXY_URL'),
    'relay_url' => env('PRONTARO_RELAY_URL'),
],

Después pasa el proxy con Http::withOptions y encadena ->throw() para que los errores 4xx/5xx lancen excepciones:

php
use Illuminate\Support\Facades\Http;

$ip = Http::withOptions([
    'proxy' => config('services.prontaro.proxy_url'),
])
    ->connectTimeout(5)
    ->timeout(15)
    ->get('https://api.ipify.org')
    ->throw()
    ->body();

Para no repetir withOptions en cada controlador, registra una macro reutilizable Http::macro('prontaro', ...) dentro de AppServiceProvider::boot().

Errores frecuentes en PHP

  • libcurl antiguo con proxies https://. libcurl < 7.52 falla con CURLE_UNSUPPORTED_PROTOCOL. Actualiza o usa http://user:pass@<entry>.prontaro.io:8080 (HTTPS destino sigue cifrado extremo a extremo).
  • Workers persistentes (Octane, FrankenPHP, Horizon). A diferencia de PHP-FPM, los workers permanecen activos entre solicitudes: reutiliza un único Client de Guzzle para mantener keep-alive y no agotar el límite de conexiones simultáneas.
  • Estado HTTP sin comprobar en cURL nativo. curl_exec() devuelve el cuerpo incluso en HTTP 403 o 500, así que comprueba siempre CURLINFO_RESPONSE_CODE.

Llamar a una API a través de una URL de Relay

Para evitar configurar proxy, crea un Relay en el panel (Direcciones IP) y llámalo como cualquier URL base HTTPS normal:

php
use Illuminate\Support\Facades\Http;

$response = Http::baseUrl(config('services.prontaro.relay_url'))
    ->withToken(config('services.partner.token'))
    // Incluir solo si activaste una contraseña (en el panel en Direcciones IP → Dirección relay → Acceso):
    ->withHeaders([
        'Prontaro-Relay-Auth' => config('services.prontaro.relay_auth'),
    ])
    ->timeout(15)
    ->get('/v1/status')
    ->throw()
    ->json();

Consulta URL de Relay para detalles sobre la terminación TLS y los límites.

Siguientes pasos

¿Hay algún error en esta página? Escríbenos

Escríbenos

Indica tu correo para que podamos responderte.Introduce un correo válido, por ejemplo name@company.example.
Cuéntanos qué ha pasado o qué necesitas.hasta 2000 caracteres

¡Enviado! Responderemos a .

O escribe a support@prontaro.com