使用laravel提交表单后需要调用该函数

时间:2018-05-22 09:31:27

标签: laravel laravel-5 laravel-4 laravel-5.2

提交表单后需要调用该函数。在我的控制器中包含两个api调用。我需要调用第一个API并从中获取“sld”值,并在执行第二个API之后将“sld”值传递给第二个API Url。但我同时得到了两个api输出。请建议在显示第二个API数据后调用第一个api的任何解决方案。

我的控制器代码

    public function domaincheck(Request $request)
       {
    ini_set('max_execution_time', 300);
    //set_time_limit(300);
        $sld = $request['sld'];
        $tld = $request['tld'];
        $response = file_get_contents('https://reseller.enom.com/interface.asp?command=check&sld='. $sld .'&tld='. $tld .'&uid=resellid&pw=resellpw&responsetype=xml');  
        $data = simplexml_load_string($response);
        $configdata   = json_encode($data);
        $final_data = json_decode($configdata,true);// Use true to get data in array rather than object
        // dd($final_data);



        $response1 = file_get_contents('http://reseller.enom.com/interface.asp?command=GETNAMESUGGESTIONS&uid=resellid&pw=resellpw&SearchTerm='. $sld .'&MaxResults=50&ResponseType=XML');       
        $data1 = simplexml_load_string($response1);
        $configdata1   = json_encode($data1);
        $final_data1 = json_decode($configdata1,true);// Use true to get data in array rather than object
        //dd($final_data1);

        }

我的观看代码

 <form class="form-horizontal" method="get"> 


  <div class="form-group">
     <div class=" col-lg-2"></div>
     <div class="col-lg-8">
         <div class="input-group m-b">
            <span class="input-group-addon" style="padding-left:10px; background-color: #999;" class='unclickable'>www</span>
            <input type="text" name="sld" class="form-control">
  <span class="input-group-addon">

  <select class="form-control" name="tld" style="width: 100px;">

  <?php $j = 0; ?>
  @foreach($final_data2['tldlist']['tld'] as $value)
  <?php $j++; ?> 
  @endforeach

  @for ($i = 0; $i < $j-1;)
  <option value="{{($final_data2['tldlist']['tld'][$i]['tld'])}}">{{($final_data2['tldlist']['tld'][$i]['tld'])}}</option>

  <?php $i++; ?> 
  @endfor


      </select>  
      </span>
      <span class="input-group-addon">
      <button type="submit" class="btn btn-sm btn-success" >Submit</button>  
      </span>
      </div>
      <p class="text-center text-dark customFont" >
                    @foreach($final_data as $key=>$value)
                      @if($key=='DomainName')
                        <b>{{$value}}</b> <b>-</b>
                      @endif 
                      @if($key=='RRPText')
                          <b>{{$value}}</b>
                      @endif
                    @endforeach
                </p>



  @foreach($final_data1['DomainSuggestions']['Domain'] as $value)

  {{$value}}<br>

  @endforeach

请建议解决此问题的任何解决方案

1 个答案:

答案 0 :(得分:1)

您应该将这两个API调用作为函数存储到 app 目录下的单独文件夹中。例如,在 \ app \ Repositories 目录下,您可以创建两个单独的php文件并将api调用存储为函数。在这样做时,请采用基于类的方法。请参阅以下示例,在 DomainSuggestion.php

    <?php

        namespace App\Repositories;


        class DomainSuggestion
        {
            function getdomain(&$domainArray)
            {
               $response = file_get_contents('https://reseller.enom.com/interface.asp?command=check&sld='. $domainArray[0] .'&tld='. $domainArray[1] .'&uid=resellid&pw=resellpw&responsetype=xml');
    $data = simplexml_load_string($response);
            $configdata   = json_encode($data);
            $final_data = json_decode($configdata,true);
return $final_data;
}
}

并在您的控制器功能中

use App\Repositories\DomainSuggestion;

     $sld = $request['sld'];
        $tld = $request['tld'];
        $domainArray = array($sld, $tld);

        $dataObject = new DomainSuggestion();
        $result = $dataObject->getdomain($domainArray);

初始化此类的对象并调用该函数(在控制器内)。来自第一个函数的返回值可以用于第二个调用。您还可以将所需参数传递给函数。别忘了将函数目录包含在控制器中(使用App \ Repositories \ ClassName;)