Advertisement
am_dot_com

CN20210326

Mar 26th, 2021 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.97 KB | None | 0 0
  1. runtime: php74
  2.  
  3. #entrypoint: serve test.php
  4. entrypoint: serve index.php
  5.  
  6. **
  7.  
  8.  
  9. <?php
  10. //this is the front controller
  11. switch (@parse_url($_SERVER['REQUEST_URI'])['path']) {
  12.     /*
  13.     case '/batatas":
  14.         require '2.php";
  15.         break;
  16.     */
  17.     case '/':
  18.         require 'form.php';
  19.         break;
  20.    
  21.     //because the form's action is just "search"
  22.     case '/search':
  23.         require 'curl1.php';
  24.         break;
  25.    
  26.     case "/test":
  27.         require 'test.php';
  28.         break;
  29.     default:
  30.         http_response_code(404);
  31.         exit('Not Found');
  32. }
  33.  
  34. ***************
  35.  
  36.  
  37. <?php
  38. require_once "vendor/autoload.php";
  39.  
  40. define (
  41.     "USER_AGENT_STRING_MOZ47",
  42.     "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"
  43. );
  44.  
  45. /*
  46.  * recebe source code HTML
  47.  * retorna um array, indexado por URL, das âncoras encontradas
  48.  * [
  49.    "https://site.com/blabla" => "As melhores batatas",
  50.    ...
  51.    "http://outro" => "outra âncora"
  52.   ]
  53.  */
  54.  
  55. function extractAsFromHtml(
  56.     string $pStrHtml
  57. ){
  58.     $ret = [];
  59.     $doc = new \DOMDocument();
  60.     if ($doc){
  61.         $bTrueOrFalse = @$doc->loadHTML($pStrHtml);
  62.         if ($bTrueOrFalse){
  63.             $as = $doc->getElementsByTagName('a');
  64.             foreach ($as as $a){
  65.                 $href = $a->getAttribute('href');
  66.                 $anchor = $a->nodeValue;
  67.                 //$ret[] = ['href'=>$href, 'anchor'=>$anchor];
  68.                 $bNew = !array_key_exists($href, $ret);
  69.                 if ($bNew) $ret[$href] = $anchor;
  70.             }
  71.         }
  72.     }
  73.     return $ret;
  74. }//extractAsFromHtml
  75.  
  76. //$ch = curl_init("https://www.google.com/search?q=Alain+Prost");
  77.  
  78.  
  79. $strExpressaoPelaQualSeProcura = $_REQUEST['nameTextSearch'];
  80. $strExpressaoPelaQualSeProcura = trim($strExpressaoPelaQualSeProcura);
  81. $strExpressaoPelaQualSeProcura = urlencode($strExpressaoPelaQualSeProcura);
  82.  
  83. $ch = curl_init("https://www.google.com/search?q=".$strExpressaoPelaQualSeProcura);
  84.  
  85. if ($ch){
  86.     $bTrueOrFalse = curl_setopt($ch, CURLOPT_USERAGENT, USER_AGENT_STRING_MOZ47);
  87.  
  88.     $bTrueOrFalse = curl_setopt($ch, CURLOPT_HTTPGET, true); //GET REQUEST
  89.     $bTrueOrFalse = curl_setopt($ch, CURLOPT_POST, false); //POST REQUEST
  90.  
  91.     $bTrueOrFalse = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //obter os dados na origem
  92.     $bTrueOrFalse = curl_setopt($ch, CURLOPT_ENCODING, ""); //encoding automático
  93.     $bTrueOrFalse = curl_setopt($ch, CURLOPT_VERBOSE, true); //+ informação
  94.  
  95.     $bTrueOrFalse = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //curl segue http-redirections
  96.     $bTrueOrFalse = curl_setopt($ch, CURLOPT_MAXREDIRS, 2); //curl estabelece limite máximo para redireções
  97.  
  98.     $dataAtSourceOrJustABooleanIfReturnTransferIsFalse = curl_exec($ch);
  99.  
  100.     $as = extractAsFromHtml($dataAtSourceOrJustABooleanIfReturnTransferIsFalse);
  101.     var_dump ($as);
  102.  
  103.     //echo $dataAtSourceOrJustABooleanIfReturnTransferIsFalse;
  104. }
  105. else{
  106.     echo "Could not init the CURL object";
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement