将php函数存储在变量中

时间:2011-12-11 07:34:37

标签: php curl

我正在使用php创建API密钥。我得到了一点但我想在变量中存储一个函数。但它反而一直回声。我希望函数应该存储在一个变量中,当变量被回显时,它的函数应该被回显。我的代码是:

客户端:

function get_cat($id){
    // Initialising authorisation to access api keys        
    $ch = curl_init("http://localhost/api/category.php?id=".$id);
    curl_setopt($ch, CURLOPT_COOKIE, "log=".$_SESSION['log']);
    // Executing script to set login session
    curl_exec($ch);
    // Closing curl connection session
    curl_close($ch);
}

服务器:

    // Fetching data from database
    $query = mysql_query("SELECT * FROM cat WHERE id={$id}", $con) or die("Sorry Cannot Connect: ".mysql_error());
    echo json_encode(mysql_fetch_assoc($query));

客户端:

$api = new API('test', 'test');
$res = $api->get_cat(2);

现在,即使我在$res变量中分配了该函数,它也会回声。反正有没有阻止它?因为我希望用户将函数存储在变量中,并使用该变量在其中任何地方显示这些内容。

1 个答案:

答案 0 :(得分:1)

默认情况下,curl会将内容添加到stdout,例如页面缓冲区,除非您请求它从curl_exec返回。

试试这个:

function get_cat($id){
    // Initialising authorisation to access api keys        
    $ch = curl_init("http://localhost/api/category.php?id=".$id);
    curl_setopt($ch, CURLOPT_COOKIE, "log=".$_SESSION['log']);
    curl_setopt($ch, CURLOPT_HEADER, 0); //2 Not inc the http headers
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    // Executing script to set login session
    $html = curl_exec($ch);
    // Closing curl connection session
    curl_close($ch);
    return $html;
}
相关问题