Last active
          August 29, 2015 14:04 
        
      - 
      
- 
        Save renanlara/6c83b6f83adcc40dfd6b to your computer and use it in GitHub Desktop. 
    Puxar dados de uma div de site via php
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| // Habilita todas mensagens de erros que possa acontecer | |
| ini_set('display_errors', 1); | |
| error_reporting(E_ALL); | |
| // Declaração de variáveis | |
| $encontrouDiv = false; | |
| // Pega o conteúdo do site (retornado como string) e coloca dentro de uma variável | |
| $urlContent = file_get_contents('http://www.url.com.br/'); | |
| // Pega tudo o que há na DIV com id='conteudo' com id exp utilizando expressão regular | |
| $reg = "/<div.*id='conteudo'.*>(.|\s)+?<\/div>/i"; | |
| if(preg_match($reg, $urlContent, $resultado)){ | |
| $encontrouDiv = true; | |
| $conteudoDiv = $resultado[0]; | |
| } | |
| ?> | |
| <!DOCTYPE html> | |
| <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> | |
| <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> | |
| <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> | |
| <!--[if gt IE 8]><!--> <html lang="pt-BR" class="no-js"> <!--<![endif]--> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
| <title>Puxar dados de um site via php</title> | |
| <meta name="description" content=""> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <!--[if lt IE 9]> | |
| <script src="js/vendor/html5-3.6-respond-1.1.0.min.js"></script> | |
| <![endif]--> | |
| </head> | |
| <body> | |
| <div> | |
| <?php | |
| if($encontrouDiv){ | |
| echo $conteudoDiv; | |
| }else{ | |
| echo 'Div não encontrada!'; | |
| } | |
| ?> | |
| </div> | |
| </body> | |
| </html> | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment