Je voudrais tester si un lien existe
pas s'il est correct http://www.dajij.com
mais tester l'existence d'un lien comme http://www.6dfdsf6dfds65fdsfdsf.com
MERCI BEAUCOUP POUR TOUT A L'EQUIPE![]()
Je voudrais tester si un lien existe
pas s'il est correct http://www.dajij.com
mais tester l'existence d'un lien comme http://www.6dfdsf6dfds65fdsfdsf.com
MERCI BEAUCOUP POUR TOUT A L'EQUIPE![]()
Comment peut-on tester avec cette fonction ?
A défaut d'avoir des détails, pourrait-on avoir le code correspondant ?
Si vous voulez vérifier l'existence d'un document avec cURL :
Même chose avec fopen (requiert allow_url_fopen à On) et une version 5 de PHP :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 function check_url($url, $timeout = 10, $maxredirs = 10) { $ret = FALSE; $ch = curl_init($url); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_NOBODY, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_MAXREDIRS, $maxredirs); if (curl_exec($ch)) { $ret = curl_getinfo($ch, CURLINFO_HTTP_CODE); } curl_close($ch); return ($ret == 200); }
Ces deux méthodes présentent l'avantage, par rapport à la fonction fsockopen, de gérer les redirections.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 function tester_url($url, $timeout = 10, $maxredirs = 10) { $options = array( 'http' => array( 'max_redirects' => $maxredirs, // PHP 5.1.0 et + 'method' => 'HEAD', 'header' => '', 'timeout' => $timeout // Effectif pour les versions 5.2.1 et supérieures ) ); $contexte = stream_context_create($options); @ $fp = fopen($url, 'r', FALSE, $contexte); if (!$fp) { return FALSE; } $meta = stream_get_meta_data($fp); fclose($fp); if (preg_match('#^HTTP/1\.[01] ([0-9]{3})#m', implode(PHP_EOL, array_reverse($meta['wrapper_data'])), $m)) { $ret = $m[1]; } return ($ret == 200); }
Partager