2

This is my code below for a twiiter app. I tried to rectify the error but to no avail

Fatal error: Call to undefined function curl() in C:\wamp\www\tweet\twitter.php on line 47

What do I have to edit in my code?

<?php
require_once('TwitterAPIExchange.php');

$settings = array(
    'oauth_access_token' => "",
    'oauth_access_token_secret' => "",
    'consumer_key' => "",
    'consumer_secret' => ""
);

$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";

$requestMethod = "GET";

if (isset($_GET['user']))  {$user = $_GET['user'];}  else {$user  = "iagdotme";}
if (isset($_GET['count'])) {$user = $_GET['count'];} else {$count = 20;}

$getfield = "?screen_name=$user&count=$count";
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);

if (is_array($string)) 
{
    foreach($string as $items)
    {
        echo "Time and Date of Tweet: ".$items['created_at']."<br />";
        echo "Tweet: ". $items['text']."<br />";
        echo "Tweeted by: ". $items['user']['name']."<br />";
        echo "Screen name: ". $items['user']['screen_name']."<br />";
        echo "Followers: ". $items['user']['followers_count']."<br />";
        echo "Friends: ". $items['user']['friends_count']."<br />";
        echo "Listed: ". $items['user']['listed_count']."<br /><hr />";
    }
}

$api = "http://api.twitter.com/1/users/show.xml?screen_name=";
$users = file("users.txt", FILE_IGNORE_NEW_LINES);

$i = 0;
if (is_array($users))
{
    foreach($users as $user)
    {
        $data = curl("$api$user");
        preg_match("#<description>(.*?)</description>#is", $data, $matches);
        $bio[$i]["user"] = $user;
        $bio[$i]["description"] = $matches[1];
        $i++;
    }

    function curl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_close($ch);
        return curl_exec($ch);
    }

}
hakre
  • 193,403
  • 52
  • 435
  • 836
Lester Pereira
  • 325
  • 1
  • 5
  • 13

5 Answers5

3

You can only use functions after you've defined them. In your case the curl() function is defined afterwards you call it because you've placed it into an if-conditional so it became a conditional function definition which are postponed.

Change your code to move it out of the condition so it get's defined unconditionally, for example on top of the file right below the require line.

<?php
require_once('TwitterAPIExchange.php');

function curl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_close($ch);
    return curl_exec($ch);
}

As this is now always defined when the code loads (not wrapped into a condition), the function is defined when the file parses, not when it executes. Because of that you could also place it at the bottom of the file. However take care that you can not place it into a condition.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • I'm now getting the following errors: 1)Warning: curl_exec(): 12 is not a valid cURL handle resource and 2)Notice: Undefined offset 1 – Lester Pereira Oct 07 '13 at 09:58
  • That sounds like your original question about the undefined error is solved. Please accept the answer as it helped you. the undefined index error has been asked and explained about earlier here, it's most often far easier to solve: [PHP: “Notice: Undefined variable” and “Notice: Undefined index”](http://stackoverflow.com/q/4261133/367456). Also you might be interested to learn about the PHP error reference we keep on Stackoverflow: [Reference - What does this error mean in PHP?](http://stackoverflow.com/q/12769982/367456) often worth for some first direction (not all errors covered but many). – hakre Oct 07 '13 at 10:59
3

Move the declaration of the curl() function outside the if(){} block that contains it, like this.

if (is_array($users))
{
  foreach($users as $user)
  {
    $data = curl("$api$user");
    preg_match("#<description>(.*?)</description>#is", $data, $matches);
    $bio[$i]["user"] = $user;
    $bio[$i]["description"] = $matches[1];
    $i++;
  }
}

function curl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
  • I'm now getting the following errors: 1)Warning: curl_exec(): 12 is not a valid cURL handle resource and 2)Notice: Undefined offset 1 – Lester Pereira Oct 07 '13 at 10:01
  • 1
    Your original code closes the `curl` handle before you execute `curl_exec`. You need to reverse the order of those two statements. I have edited my aswer above accordingly. Note that your code doesn't check the return value of `curl()`. If there's an error subsequent code will fail. –  Oct 07 '13 at 10:21
  • Thanks Mike. I'm still getting the 2nd error of undefined offset 1 ->$bio[$i]["description"] = $matches[1]; – Lester Pereira Oct 07 '13 at 10:28
  • That suggests that your `preg_match` isn't finding what it's looking for so there's nothing in `matches[1]`. You'll need to examine the returned data from your `curl` calls to see what's being retrieved, and debug the RegExp against that. Parsing HTML or XML with Regexp is notoriously unreliable. I can't really help further. –  Oct 07 '13 at 10:42
  • A PS: I took a look at the URL you are using. It returns a message from Twitter indicating that the version of the API you're using is discontinued. There's no `` element, so that's why your RegExp is failing. You'll need to look again at the API since the new one doesn't provide XML output, and requires some authentication –  Oct 07 '13 at 10:55
0

curl is an extension that needs to be installed.

http://www.php.net/manual/en/curl.setup.php

Step By Step Guide for install curl

Shakti Patel
  • 3,762
  • 4
  • 22
  • 29
0

go to your xampp installation folder (C:\www\xampp\php) and then select the file php.ini (notepad file) in php folder.

;extension=php_curl.dll

you can see this extension inside the file. u should remove the semicolon in that particular sentence and then restart the apache. i hope it will work...

Karuppiah RK
  • 3,894
  • 9
  • 40
  • 80
0
function curl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_close($ch);
    return curl_exec($ch);
}

if (is_array($users))
    {
        foreach($users as $user)
        {
            $data = curl("$api$user");
            preg_match("#<description>(.*?)</description>#is", $data, $matches);
            $bio[$i]["user"] = $user;
            $bio[$i]["description"] = $matches[1];
            $i++;
        }

    }
Carlos
  • 228
  • 3
  • 10