从调用的函数返回值

时间:2017-02-11 11:31:52

标签: php

在以下自定义功能中......

function get_txtlocal_balance() {
    $username = variable_get('sms_txtlocal_email');
    $hash = variable_get('sms_txtlocal_password');
    $data = array('username' => $username, 'hash' => $hash);
    $ch = curl_init('http://api.txtlocal.com/balance/');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    $balance = json_decode($response, true);
    echo $balance['balance']['sms'];
}

我从文本本地帐户返回余额。现在我尝试将这种平衡回应到位......

$balance = get_txtlocal_balance();
$title = 'SMS Integration - Current Balance: ' . $balance;

<span><?php echo $title;?></span>

但是返回的值并没有出现在正确的位置,它总是返回页面的顶部,任何人都可以发现我做错了什么?

1 个答案:

答案 0 :(得分:1)

替换以下代码:

function get_txtlocal_balance() {
$username = variable_get('sms_txtlocal_email');
$hash = variable_get('sms_txtlocal_password');
$data = array('username' => $username, 'hash' => $hash);
$ch = curl_init('http://api.txtlocal.com/balance/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$balance = json_decode($response, true);
return $balance['balance']['sms'];
}