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
- Abre Direcciones IP en tu panel de Prontaro (my.prontaro.com/ips) y selecciona tu IPv4.
- Copia la URL
HTTPS(:443) oSOCKS5(:1080) en Credenciales del proxy.
Exporta la cadena del proxy en PRONTARO_PROXY_URL:
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:
composer require guzzlehttp/guzzle<?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.10Guzzle 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
// 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
// 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:
// 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:
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
libcurlantiguo con proxieshttps://.libcurl< 7.52 falla conCURLE_UNSUPPORTED_PROTOCOL. Actualiza o usahttp://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
Clientde Guzzle para mantener keep-alive y no agotar el límite de conexiones simultáneas. - Estado HTTP sin comprobar en
cURLnativo.curl_exec()devuelve el cuerpo incluso en HTTP403o500, así que comprueba siempreCURLINFO_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:
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
- curl y variables de entorno — diagnóstico de errores
407y403concurl -v. - Proxy HTTP y SOCKS5 — configuración de
access,targetsy rotación de credenciales. - Java — ejemplos con
HttpClient,OkHttpy SpringRestClient.
¿Hay algún error en esta página? Escríbenos