2016-03-24 1 views
1

Ci-dessous mon code php pour sortir le texte sous l'ID = Résumé. Eh bien ce script fonctionne bien pour d'autres webistes mais pas pour wikipedia. J'ai aussi collé l'erreur que je reçois ci-dessous. Wikipedia limite-t-il le script de l'analyseur? Si oui, y a-t-il une solution pour analyser et obtenir le contenu du wiki? Merci d'avance.PHP crawler ne fonctionne pas pour wikipedia

<?php 


function getElementByIdAsString($url, $id, $pretty = true) { 
    $doc = new DOMDocument(); 

    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36'); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    $result = curl_exec($ch); 


// var_dump($doc->loadHTMLFile($url)); die; 
error_reporting(E_ERROR | E_PARSE); 
    if(!$result) { 
     throw new Exception("Failed to load $url"); 
    } 
    $doc->loadHTML($result); 
    // Obtain the element 
    $element = $doc->getElementById($id); 

    if(!$element) { 
     throw new Exception("An element with id $id was not found"); 
    } 

    if($pretty) { 
     $doc->formatOutput = true; 
    } 

    // Return the string representation of the element 
    return $doc->saveXML($element); 
} 

//Here I am dispalying the output in bold text 
echo getElementByIdAsString('https://en.wikipedia.org/wiki/A_Brief_History_of_Time', 'Summary'); 
?> 

Erreur:

Fatal error: Uncaught exception 'Exception' with message 'Failed to load http://en.wikipedia.org/wiki/A_Brief_History_of_Time' in C:\xampp\htdocs\example2.php:25 Stack trace: #0 C:\xampp\htdocs\example2.php(49): getElementByIdAsString() #1 {main} thrown in C:\xampp\htdocs\example2.php on line 25 
+0

Quelle est l'erreur CURL que vous obtenez? –

+0

Code d'état Curl? –

+0

http://php.net/manual/fr/function.curl-error.php cette fonction renverrait l'erreur du problème de certificat SSL CURL –

Répondre

1

il est semble que ce soit une copie de celle-ci: php crawler for wiki getting error

la raison est que l'essai boucle pour vérifier cert si juste d'ajouter:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

supprimer le problème, mais je sugest à utiliser tout cela

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);