【问题标题】:function call returning first character of array output函数调用返回数组输出的第一个字符
【发布时间】:2021-01-18 15:20:54
【问题描述】:

抱歉,不完全确定如何措辞。

我有以下调用 2 个函数的 PHP 脚本。两者都输出一个已解码的数组。我只需要每个数组的 1 部分。

{
<?php
require_once('sipTrunkDomain.php');

//setup curl connection
//get list of SIP Trunk domains, uses CURL GET, and only return the domain header
function getTrunkDomain(){
$json = sipTrunkDomain();
//$r = json_encode($json);
$arr = json_decode($json,true);
    return $arr;
  }
//returns list of trunk domains
$trunks = getTrunkDomain();
//sets domains as an array to be passed to next function
if(!is_array($domain))
$domain = array();
foreach ($trunks as $trunk){
  $domain[] = $trunk['domain'];

}
//print_r($domain);

//uses domain name from above as a variable for the domain information API call
foreach ($domain as $dom){
  if (empty($dom)){
    echo "No Territory";
  }else{
  print $dom . " - Territory is " . getDomTerr($dom)[0] . "\n";
  }
}
?>

第一个函数 getTrunkDomain() 从一个函数中提取客户端名称列表

第二个函数 getDomTerr() 使用第一个函数的输出作为 CURL 输入,为客户端输出一个全局帐户。

我的脚本工作正常,但它只返回 getDomTerr() 结果的第一个字符。

功能 1

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

if ($err) {
  echo "Error #:" . $err;
} else {
    return $response;
}
}

功能2

$response = curl_exec($curl);
  $err = curl_error($curl);
  curl_close($curl);
  
  if ($err) {
    echo "Error #:" . $err;
  } else {
      $temp = json_decode($response,true);
      //print_r($temp);
      return $temp[0]['territory'];
      
  }
  }

运行时的输出为:

domain1 - Territory is u
domain2 - Territory is a
domain3 - Territory is a
domain4 - Territory is u

其中“Territory is u”应该有一个多字符的结果,而不仅仅是字母 u

知道为什么会这样。如果我使用“域”的手动输入直接调用函数 2,它将返回全长字符串。

由于存在敏感信息,我无法显示完整的 curl CURLOP_URL,但它是失眠呼叫的输出,我在每个单独的呼叫中都在寻找正确的输出。

我是这方面的菜鸟,所以寻求帮助。

【问题讨论】:

    标签: php arrays json curl scripting


    【解决方案1】:

    原来我已经在函数 2 中设置了 ['territory']

    函数已经设置了区域的返回,我只引用输出的 0 位置,而使用 getDomTerr($dom)[0] 它只返回第一个字母。

    删除了 [0],现在返回完整的区域名称

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多