Necesito file_get_contents
para ser tolerante a fallas, como en, si el $url
alimentado devuelve un , para contarme sobre esto antes de que salga una advertencia. Se puede hacer esto?¿File_get_contents fallará correctamente?
9
A
Respuesta
16
Cualquier función que use el contenedor HTTP para acceder a un archivo remoto como si fuera local generará automáticamente una variable local llamada $http_response_header
en el ámbito local. Puede acceder a esa variable para encontrar información sobre lo que sucedió en una llamada al fopen
, file_get_contents
... en un archivo remoto.
Puede suprimir la advertencia utilizando @
: @file_get_contents
.
Si no se preocupan por lo que el error fue, puede utilizar @file_get_contents
y comparar el resultado con false
:
$content = @file_get_contents(url);
if ($content === false) { /* failure */ } else { /* success */ }
6
Se podría hacer una solicitud adicional (HEAD) para averiguar en primer lugar, por ejemplo,
$response = get_headers($url);
if($response[1] === 'HTTP/1.1 200 OK') {
$content = file_get_contents($url);
}
O se puede decir file_get_contents
ignorar los errores y la fuerza-recoger el resultado de la $url
modificando el contexto de secuencia:
echo file_get_contents(
'http://www.example.com/foo.html',
FALSE,
stream_context_create(array('http'=>array('ignore_errors' => TRUE)))
)
Cuestiones relacionadas
- 1. ¿Cuándo fallará ofstream :: open?
- 2. Predecir que el programa fallará
- 3. file_get_contents() Error
- 4. file_get_contents cuelga
- 5. Ruby file_get_contents equivalent
- 6. ¿Alternativa a file_get_contents?
- 7. php file_get_contents y &
- 8. file_get_contents devuelve 403 prohibido
- 9. Función equivalente para file_get_contents()?
- 10. PHP: file_get_contents ($ loc) falla
- 11. file_get_contents recibir cookies
- 12. file_get_contents() para URLs cortas
- 13. file_get_contents devuelve cadena vacía
- 14. file_get_contents y php code
- 15. File_get_contents no funciona?
- 16. urlencode y file_get_contents
- 17. Mostrar imagen usando file_get_contents
- 18. ¿En qué situaciones fallará el campo field.setAccessible (true) de Java?
- 19. PHP - Desplazamiento a través de JSON file_get_contents
- 20. PHP file_get_contents con php intacto?
- 21. Solr PHP client vs file_get_contents?
- 22. file_get_contents - caracteres especiales en URL
- 23. Equivalente a file_get_contents() con CURL?
- 24. PHP file_get_contents ($ url) slow performance
- 25. alternativa más rápida a file_get_contents()
- 26. file_get_contents detrás de un proxy?
- 27. file_get_contents() Descompone caracteres UTF-8
- 28. file_get_contents() pudo abrir la secuencia:
- 29. PHP - Enviar cookie con file_get_contents
- 30. JSON a PHP Matriz usando file_get_contents
¡Exactamente lo que necesitaba, gracias! –