tags:

views:

56

answers:

3

I am in the process of making a page fetch script & I was using curl for it. I used the function:

get_data($url);

but I always get the error:

Fatal error: Call to undefined function get_data() in D:\wamp\www\grab\grab.php on line 16

I am using WAMP server and I have enabled curl extentions in all the ini files, I have checked the extensions directory path but everything seems fine and I am still stuck.

Could anyone please tell me what am I doing wrong?

Thanks.

+3  A: 

There is not get_data function!!

You'll have to code one yourself as:

function get_data($url) {
   $ch = curl_init();    
   curl_setopt($ch,CURLOPT_URL,$url);
   $data = curl_exec($ch);
   curl_close($ch);
   return $data;
}
codaddict
totally worked! my bad. First attempt at curl.
Aayush
+2  A: 

Use this function. There is no get_data function:

<?php
function file_get_contents_curl($url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

echo file_get_contents_curl("http://www.google.com/");
?>
shamittomar
+2  A: 

I'm not an expert using cUrl... but I've never heard about that get_data() function being part of cUrl.

http://es.php.net/manual/en/book.curl.php

To make a query you must create a curl instance:

$curl = curl_init( $url );

And then you can execute the query with:

curl_exec( $curl );

...basically.

With curl_setopt (http://es.php.net/manual/en/function.curl-setopt.php) you can set various options (I'm sure you will need to).

One option specially useful that makes curl even easer to use is:

curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );

This makes curl_exec() to return a string with the content of the response.

So the whole example ends like:

   $url = 'http://www.google.com';
   $curl = curl_init( $url );
   curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
   $content = curl_exec( $curl );

   echo "The google.com answered=".$content;
clinisbut