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

Php 致命错误:调用未定义的函数curl(),php,twitter,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' => "",

下面是我的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);
    }

}

curl是一个需要安装的扩展


只有在定义了函数之后才能使用它们。在您的例子中,
curl()
函数是在您调用它之后定义的,因为您将它放入了if条件so中

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

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

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;
}
if(is_数组($users))
{
foreach($users作为$user)
{
$data=curl(“$api$user”);
preg#u match(“#(.*)#is”,$data,$matches);
$bio[$i][“用户”]=$user;
$bio[$i][“说明”]=$matches[1];
$i++;
}
}
函数curl($url)
{
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result=curl\u exec($ch);
卷曲关闭($ch);
返回$result;
}

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

);extension=php\u curl.dll

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

函数curl($url)
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++;
        }

    }
{ $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$URL); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 卷曲关闭($ch); 返回curl_exec($ch); } if(is_数组($users)) { foreach($users作为$user) { $data=curl(“$api$user”); preg#u match(“#(.*)#is”,$data,$matches); $bio[$i][“用户”]=$user; $bio[$i][“说明”]=$matches[1]; $i++; } }
检查您的服务器中是否启用了curl扩展。我启用了它,但仍然得到了errorsHi Move curl函数。那就试试吧,谢谢。我现在得到这个未定义的偏移量1错误-->$bio[$I][“description”]=$matches[1];在这种情况下,preg_match未能加载$matches1中的值。Pl check…我现在得到以下错误:1)警告:curl_exec():12不是有效的curl句柄资源,2)注意:未定义的偏移量1听起来像是关于未定义错误的原始问题已解决。请接受这个答案,因为它对你有帮助。前面已经询问并解释了未定义索引错误,通常更容易解决:。另外,您可能有兴趣了解我们在Stackoverflow上保留的PHP错误参考:对于某些第一个方向(不是所有的错误,而是许多错误)通常是值得的:12不是有效的cURL句柄资源,2)注意:未定义的偏移量1在执行
cURL\u exec
之前,原始代码将关闭
cURL
句柄。您需要颠倒这两条语句的顺序。我已经相应地编辑了上面的答案。请注意,您的代码没有检查
curl()
的返回值。如果出现错误,后续代码将失败。谢谢Mike。我仍然得到未定义偏移量1->$bio[$I][“description”]=$matches[1]的第二个错误;这表明您的
preg\u match
没有找到它要查找的内容,因此
matches[1]
中没有任何内容。您需要检查从
curl
调用返回的数据,以查看检索到的内容,并根据该内容调试RegExp。用Regexp解析HTML或XML是出了名的不可靠。答:我看了你正在使用的网址。它从Twitter返回一条消息,指示您正在使用的API版本已停止。没有
元素,所以这就是您的RegExp失败的原因。您需要再次查看API,因为新API不提供XML输出,并且需要一些身份验证