PHP函数返回一个变量?

时间:2014-02-03 06:55:11

标签: php api function curl return

我很难找到如何让函数返回一个我可以在函数外部使用的变量,我试过了

return $response 

我试过

   return $response = $anothervariable

但是我能够让它工作的唯一方法是回应响应并将html放在那里,我知道有更好的方法来做到这一点,问题是我无法弄明白。任何帮助将不胜感激!

function OpenCNAM($ph) {

$opencnamSID = '*****';
$opencnamTOKEN = '*****';
$query = "https://api.opencnam.com/v2/phone/".$ph."?format=text";
if(isset($opencnamSID)) { $query = "https://".$opencnamSID.":".$opencnamTOKEN."@api.opencnam.com/v2/phone/".$ph."?format=text"; }
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $query);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 2);   
curl_setopt ($ch, CURLOPT_TIMEOUT, 3);      
$response = curl_exec($ch);
curl_close($ch);

if($response != "") {    
  echo '<span title="'.$response.'"> 888-452-1505</span>';

} else {
    echo 'Nada';
}

}

OpenCNAM('8884521505');

5 个答案:

答案 0 :(得分:2)

return $response;

是正确的陈述。如果程序流程达到该点,它将返回$ response变量。

您还可以通过引用传递参数,例如&amp; $ any_variable,在函数内部设置欲望值,并在函数调用后在函数外部播放。

答案 1 :(得分:1)

像这样重新修改你的代码。

function OpenCNAM($ph) {

$opencnamSID = '*****';
$opencnamTOKEN = '*****';
$query = "https://api.opencnam.com/v2/phone/".$ph."?format=text";
if(isset($opencnamSID)) { $query = "https://".$opencnamSID.":".$opencnamTOKEN."@api.opencnam.com/v2/phone/".$ph."?format=text"; }
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $query);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 2);   
curl_setopt ($ch, CURLOPT_TIMEOUT, 3);      
$response = curl_exec($ch);
curl_close($ch);
$str="";
if($response != "") {    
  $str= '<span title="'.$response.'"> 888-452-1505</span>';

} else {
   $str= 'Nada';
}
return $str; //<---------- We are returning here
}

echo OpenCNAM('8884521505'); //<--- If you want to output.. or just $response = OpenCNAM('43535'); if you want to store it in a variable.

答案 2 :(得分:1)

只需返回文本然后就可以这样做

function OpenCNAM($ph) {
    //...
    //...
    if($response != "") {    
        return '<span title="'.$response.'"> 888-452-1505</span>';

    } else {
        return 'Nada';
    }

}

$foo = OpenCNAM('8884521505');

然后,你可以用$ foo

做你想做的事

答案 3 :(得分:0)

试试这个

function OpenCNAM($ph)
 {

    $opencnamSID = '*****';
    $opencnamTOKEN = '*****';
    $query = "https://api.opencnam.com/v2/phone/".$ph."?format=text";
    if(isset($opencnamSID)) { $query = "https://".$opencnamSID.":".$opencnamTOKEN."@api.opencnam.com/v2/phone/".$ph."?format=text"; }
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $query);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 2);   
    curl_setopt ($ch, CURLOPT_TIMEOUT, 3);      
    $response = curl_exec($ch);
    curl_close($ch);

    if($response != "") 
    {    
        $res =  '<span title="'.$response.'"> 888-452-1505</span>';
    }
    else 
    {
        $res =  'Nada';
    }
return $res;
}

$return_res = OpenCNAM('8884521505');
echo $return_res;

答案 4 :(得分:0)

如果您不打算在任何地方使用返回的数据,那么echo将为您做好。

PHP echo function return value vs echo inside function

回答了这个问题