致命错误:调用未定义的函数curl()

时间:2013-10-07 08:57:25

标签: php twitter

这是我的代码,下面是一个twiiter应用程序。 我试图纠正错误,但无济于事

  

致命错误:在第47行的C:\ wamp \ www \ tweet \ twitter.php中调用未定义的函数curl()

我需要在代码中编辑什么?

<?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);
    }

}

5 个答案:

答案 0 :(得分:3)

您只能在定义后使用功能。在您的情况下,curl()函数是在您调用之后定义的,因为您已将其置于if条件it became a conditional function definition which are postponed中。

更改您的代码以将其移出条件,以便无条件地定义,例如在需求行正下方的文件顶部。

<?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);
}

由于现在总是在代码加载时定义(未包装到条件中),因此在文件解析时定义函数,而不是在执行时定义函数。因此,您也可以将它放在文件的底部。但请注意不要将其置于某种状态。

答案 1 :(得分:3)

curl()函数的声明移到包含它的if(){}块之外,就像这样。

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;
}

答案 2 :(得分:0)

答案 3 :(得分:0)

转到xampp安装文件夹(C:\ www \ xampp \ php),然后在php文件夹中选择文件php.ini(记事本文件)。

; extension = php_curl.dll

你可以在文件中看到这个扩展名。你应该删除该特定句子中的分号,然后重新启动apache。我希望它会起作用......

答案 4 :(得分: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++;
        }

    }