2011-12-11 140 views
0

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

客户:

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变量它的回声。无论如何要阻止它?因为我希望用户将函数存储在一个变量中,并使用该变量在其所需的任何位置显示这些内容。

+0

我很抱歉,我没有得到你如果我的英语,然后我的坏我很抱歉...... –

回答

1

默认情况下,curl会将内容添加到标准输出,例如,页面缓冲区取消选择您请求它从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; 
} 
+0

作品十分感谢... –