使用 laravel 提交表单后需要调用该函数
Need to call the function after submit the form using laravel
提交表单后需要调用该函数。在我的控制器中包含两个 api calls.I 需要调用第一个 API 并从中获取 "sld" 值并将 "sld" 值传递给第二个 API Url 之后执行第二个 API。但是我同时得到了两个 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
请提出解决此问题的任何解决方案
您应该将这两个 API 调用作为函数存储到 app 目录下的单独文件夹中。例如,在 \app\Repositories 目录下,您可以创建两个单独的 php 文件并将 api 调用存储为函数。执行此操作时,请采用基于 class 的方法。请参阅以下示例,在 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);
初始化此 class 的对象并调用函数(在您的控制器内)。第一个函数的返回值可以用于第二个调用。您还可以将所需的参数传递给函数。不要忘记将函数目录包含到您的控制器中(使用 App\Repositories\ClassName;)
提交表单后需要调用该函数。在我的控制器中包含两个 api calls.I 需要调用第一个 API 并从中获取 "sld" 值并将 "sld" 值传递给第二个 API Url 之后执行第二个 API。但是我同时得到了两个 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
请提出解决此问题的任何解决方案
您应该将这两个 API 调用作为函数存储到 app 目录下的单独文件夹中。例如,在 \app\Repositories 目录下,您可以创建两个单独的 php 文件并将 api 调用存储为函数。执行此操作时,请采用基于 class 的方法。请参阅以下示例,在 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);
初始化此 class 的对象并调用函数(在您的控制器内)。第一个函数的返回值可以用于第二个调用。您还可以将所需的参数传递给函数。不要忘记将函数目录包含到您的控制器中(使用 App\Repositories\ClassName;)