Page 1 of 1

Undefined function error

Posted: Thu Nov 20, 2008 10:27 pm
by Discussnow
The issue seems to be that "file_get_the_contents($url) ; " is causing the error because it is not defined until after the function runs. I attempted to move the whole line below the function and as a result got a 403 error for the whole page.

Code: Select all

<?php
$para = $_GET['para'] ;
$url = "https://siteexplorer.search.yahoo.com/search?p=$para";
$scrape = file_get_the_contents($url) ;
if (isset($_Post['para'])){
function file_get_the_contents($url) {
  $ch = curl_init();
  $timeout = 10; // set to zero for no timeout
  curl_setopt ($ch, CURLOPT_URL, $url);
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $file_contents = curl_exec($ch);
  curl_close($ch);
  return $file_contents;
   }  
}
 
echo $scrape ;
?>
</html>

Re: Undefined function error

Posted: Thu Nov 20, 2008 10:55 pm
by requinix
Normally you can call functions at any time because PHP preprocesses the file and sees the function. But you put it in a conditional, so PHP has to wait until execution makes it there (if it does) before the function can be defined.

Doing it in your code doesn't make sense. In fact, your code doesn't make sense period.

Code: Select all

<?php
 
function file_get_the_contents($url) {
  $ch = curl_init();
  $timeout = 10; // set to zero for no timeout
  curl_setopt ($ch, CURLOPT_URL, $url);
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $file_contents = curl_exec($ch);
  curl_close($ch);
  return $file_contents;
   }  
 
if (isset($_REQUEST['para'])){
$para = urlencode($_REQUEST['para']) ;
$url = "https://siteexplorer.search.yahoo.com/search?p=$para";
$scrape = file_get_the_contents($url) ;
echo $scrape ;
}
 
?>
</html>