Undefined function error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Discussnow
Forum Newbie
Posts: 3
Joined: Sat Nov 15, 2008 3:46 pm

Undefined function error

Post 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>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Undefined function error

Post 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>
Post Reply