IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Utiliser "PHP Parallel" avec DOMDocument dans mon code PHP


Sujet :

Langage PHP

  1. #1
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Octobre 2013
    Messages
    452
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2013
    Messages : 452
    Points : 66
    Points
    66
    Par défaut Utiliser "PHP Parallel" avec DOMDocument dans mon code PHP
    Bonjour.

    Je souhaiterais utiliser PHP-8 Parallel (https://github.com/krakjoe/parallel. Ou encore: https://php.net/parallel) dans ma fonction follow_links:

    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
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    function followLinks($urls) {
            global $alreadyCrawled;
            global $crawling;
     
            $parser = new DomDocumentParser($page);
     
            foreach($urls as $page) {
     
                $linkList = $parser->getLinks();
     
                foreach($linkList as $link) {
                    $href = $link->getAttribute("href");
     
                    if(strpos($href, "#") !== false) {
                        continue;
                    }
                    else if(substr($href, 0, 11) == "javascript:") {
                        continue;
                    }
                    $href = createLink($href, $url);
                    if(!in_array($href, $alreadyCrawled)) {
                        $alreadyCrawled[] = $href;
                        $crawling[] = $href;
                        // Output the page title, descriptions, keywords, URL, Image, Video, etc... This output is
                        // piped off to an external file using the command line.
                        getDetails($href);
     
                    }
                }
     
            }
            // Remove an item from the array after we have crawled it.
            // This prevents infinitely crawling the same page.
            array_shift($crawling);
     
            followLinks($crawling);
     
        }
     
    $starts = ["https://website1.dn", "https://website2.dn", "https://website3.dn", "https://website4.dn"];
     
    followLinks($starts);
    Je cherche donc à traiter tous les URLs stockés dans la variable $starts à la fois de façon parallèle sachant que c'est la fonction get_details qui récupère les données de chaque URL ???

    Merci de m'aider.

  2. #2
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Octobre 2013
    Messages
    452
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2013
    Messages : 452
    Points : 66
    Points
    66
    Par défaut
    Voici ce que j'ai essayé de faire:

    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
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    function followLinks($urls) {
            global $alreadyCrawled;
            global $crawling;
     
            $parser = new DomDocumentParser($page);
     
            foreach($urls as $page) {
     
                $linkList = $parser->getLinks();
     
                foreach($linkList as $link) {
                    $href = $link->getAttribute("href");
     
                    if(strpos($href, "#") !== false) {
                        continue;
                    }
                    else if(substr($href, 0, 11) == "javascript:") {
                        continue;
                    }
                    $href = createLink($href, $url);
                    if(!in_array($href, $alreadyCrawled)) {
                        $alreadyCrawled[] = $href;
                        $crawling[] = $href;
                        $runtime = new \parallel\Runtime();
                        $crawling_arr_length = count($crawling);
                        $future = $runtime->run(function() {
                            for ($i=0; $i < $crawling_arr_length; $i++)
                                // Output the page title, descriptions, keywords, URL, Image, Video, etc... This output is
                                // piped off to an external file using the command line.
                                getDetails($href);
     
                            return "easy";
                        });
                    }
                }
     
            }
            // Remove an item from the array after we have crawled it.
            // This prevents infinitely crawling the same page.
            array_shift($crawling);
     
            followLinks($crawling);
     
        }
     
    $starts = ["https://website1.dn", "https://website2.dn", "https://website3.dn", "https://website4.dn"];
     
    followLinks($starts);
    Vous pouvez constater de la ligne 24 à 33 que j'ai essayé d'inclure le classe Runtime instanciée $runtime = new \parallel\Runtime(); mais, c'est complètement brouillon et ne fonctionne pas.

    Quel serait la manière la plus appropriée pour inclure le Parallel dans l'appel de ma fonction getDetails dans la fonction followLinks ???

Discussions similaires

  1. Réponses: 5
    Dernier message: 12/06/2022, 20h12
  2. Réponses: 2
    Dernier message: 12/06/2022, 18h31
  3. [XL-2013] Utiliser la commande ThisWorkbook.Path dans mon code
    Par CrasherSEP dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 16/06/2017, 14h57
  4. Erreur dans mon code PHP
    Par jack_1981 dans le forum Langage
    Réponses: 6
    Dernier message: 12/05/2006, 10h01
  5. Projet VB v6 : utilisation de requête SQL dans mon code
    Par MITCH31 dans le forum VB 6 et antérieur
    Réponses: 4
    Dernier message: 27/10/2005, 15h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo