2017-04-09 132 views
-2

我遇到問題。我試圖用cURL從網站獲取html。它在此模式下正常工作:PHP從網站使用cURL不起作用獲取html

$c = curl_init($url); 
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true); 
    //curl_setopt(... other options you want...) 

    $html = curl_exec($c); 

    if (curl_error($c)) 
     die(curl_error($c)); 

    // Get the status code 
    $status = curl_getinfo($c, CURLINFO_HTTP_CODE); 

    curl_close($c); 
    $html = str_get_html($html); 

但是,當我試圖把代碼中的函數,並調用它在第二時刻,不行!此代碼的函數:

function get_html_from_page($url) { 
    $c = curl_init($url); 
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true); 
    //curl_setopt(... other options you want...) 

    $html = curl_exec($c); 

    if (curl_error($c)) 
     die(curl_error($c)); 

    // Get the status code 
    $status = curl_getinfo($c, CURLINFO_HTTP_CODE); 

    curl_close($c); 
    return $html; 
} 

//echo "Il contenuto del file è: ". $html . " con status: " . $status; 
function get_last_vote_player() { 

    echo '<table border="1"><theader>'; 
    echo '<td><b>Giocatore</b></td>'; 
    echo '<td><b>Voto</b></td>'; 
    echo '<td><b>Gol fatti/subiti</b></td>'; 
    echo '<td><b>Assist</b></td>'; 
    echo '<td><b>Rigori Realizzati</b></td>'; 
    echo '</theader><tbody>'; 

    $html = str_get_html(get_html_from_page('http://www.gazzetta.it/calcio/fantanews/voti/serie-a-2016-17/')); 

    $dep = array(); 
    foreach($html->find('.magicTeamList') as $team) { 
     $squadra = $team->first_child()->first_child()->last_child()->innertext; 


     if (!in_array(ucwords($squadra),$dep)) { 
      echo '<tr><td colspan="5"><b>'.ucwords($squadra).'</b></td></tr>'; 
      foreach($team->find('.playerName') as $player) { 
       echo '<tr>'; 
       echo '<td>'.$player->find('a',0)->innertext . '</td>'; 
       //echo $player->innertext . '<br>'; 
       $voto = trim($player->next_sibling()->innertext); 
       if ($voto == '-') $voto = 'S.V.'; else $voto = floatval($voto); 
       echo '<td>' . $voto . '</td>'; 
       $golFatti = $player->next_sibling(); 
       echo '<td>'.$golFatti->next_sibling()->innertext . '</td>'; 
       $assist = $golFatti->next_sibling(); 
       echo '<td>'.$assist->next_sibling()->innertext . '</td>'; 
       $rigoriRealizzati = $assist->next_sibling(); 
       echo '<td>'.$rigoriRealizzati->next_sibling()->innertext . '</td>'; 
       echo '</tr>'; 
      } 
     } 

     $dep[] = ucwords($squadra); 
    } 
    echo '</tbody></table>'; 
} 


get_last_vote_player(); 

而且還當我試圖把它叫成這樣一類不工作:

class WebPage { 
    public $url = ''; 

    public function __construct($url) { 
     $this->url = $url; 
    } 

    protected function get_html_from_page() { 
     $c = curl_init($this->url); 
     curl_setopt($c, CURLOPT_RETURNTRANSFER, true); 
     //curl_setopt(... other options you want...) 

     $html = curl_exec($c); 

     if (curl_error($c)) 
      die(curl_error($c)); 

     // Get the status code 
     $status = curl_getinfo($c, CURLINFO_HTTP_CODE); 

     curl_close($c); 
     return $html; 
    } 
} 

class PlayerVote extends WebPage { 

    public function __construct($url) { 
     parent::__construct($url); 
    } 

    public function get_last_vote_player() { 

     echo '<table border="1"><theader>'; 
     echo '<td><b>Giocatore</b></td>'; 
     echo '<td><b>Voto</b></td>'; 
     echo '<td><b>Gol fatti/subiti</b></td>'; 
     echo '<td><b>Assist</b></td>'; 
     echo '<td><b>Rigori Realizzati</b></td>'; 
     echo '</theader><tbody>'; 

     $html = str_get_html($this->get_html_from_page('http://www.gazzetta.it/calcio/fantanews/voti/serie-a-2016-17/')); 

     $dep = array(); 
     foreach($html->find('.magicTeamList') as $team) { 
      $squadra = $team->first_child()->first_child()->last_child()->innertext; 


      if (!in_array(ucwords($squadra),$dep)) { 
       echo '<tr><td colspan="5"><b>'.ucwords($squadra).'</b></td></tr>'; 
       foreach($team->find('.playerName') as $player) { 
        echo '<tr>'; 
        echo '<td>'.$player->find('a',0)->innertext . '</td>'; 
        //echo $player->innertext . '<br>'; 
        $voto = trim($player->next_sibling()->innertext); 
        if ($voto == '-') $voto = 'S.V.'; else $voto = floatval($voto); 
        echo '<td>' . $voto . '</td>'; 
        $golFatti = $player->next_sibling(); 
        echo '<td>'.$golFatti->next_sibling()->innertext . '</td>'; 
        $assist = $golFatti->next_sibling(); 
        echo '<td>'.$assist->next_sibling()->innertext . '</td>'; 
        $rigoriRealizzati = $assist->next_sibling(); 
        echo '<td>'.$rigoriRealizzati->next_sibling()->innertext . '</td>'; 
        echo '</tr>'; 
       } 
      } 

      $dep[] = ucwords($squadra); 
     } 
     echo '</tbody></table>'; 
    } 
} 

$vote = new PlayerVote('http://www.gazzetta.it/calcio/fantanews/voti/serie-a-2016-17/'); 
$vote->get_last_vote_player(); 

無法工作,因爲當我嘗試去的網頁,它響應是「 無法到達網站」

爲什麼?

回答

1

當您嘗試調用它的一類...的錯誤是在這條線:

$ HTML = str_get_html($這個 - > get_html_from_page( 'http://www.gazzetta.it/calcio/fantanews/voti/serie-a-2016-17/'));

你不必通過URL中get_html_from_page作爲函數不採取任何輸入...網址是牽強使用這個 - $> URL ....

將其更改爲: $ HTML = str_get_html( $這 - > get_html_from_page());

它應該工作

+0

請接受答案,如果它工作 –