2016-03-24 23 views
1

Aşağıda, id = Summary altındaki metni çıkaran php kodum var. Peki bu script diğer web siteleri için iyi çalışıyor, ancak wikipedia için değil.Aşağıdaki yanıta aldığım hatayı da yaptık. Wikipedia çözümleyici komut dosyasını kısıtlıyor mu? öyleyse, ayrıştırmak ve içeriği wiki'den almak için herhangi bir çözüm var mı? Şimdiden teşekkürler.PHP tarayıcısı wikipedia için çalışmadı

<?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'); 
?> 

Hata: Bu yinelenen bir kopyası gibi

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

Aldığınız CURL hatası nedir? –

+0

Curl durum kodu? –

+0

http://php.net/manual/en/function.curl-error.php bu işlev, hatalı olanı CURL –

cevap

1

öyle görünüyor: php crawler for wiki getting error

nedeni bukle deneyin yani sadece ekleyerek sertifika doğrulamak için olmasıdır:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

sorunu kaldırın, ancak tüm bu

kullanmak için en uygun
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);