I'm building a simple domain availability checker using the Godaddy api.
I'm using Laravel 9 for the application.
It gives back available and already registered results, but if I add a string without domain extension like: somename so no .com or.org etc. then I get an ("Undefined array key "available") error message.
On my view file I only have a simple form with one input field where the user can enter the domain name to be checked.
In my controller I validate the input:
$validator = Validator::make($request->all(), [
'userDomain' => ['required', 'string', 'max:191']
]);
I also have my API keys and base url stored in variables:
$API_KEY_OTE = "apikey";
$API_SECRET_OTE = "apisecret";
$OTE_BASE_URL = "https://api.ote-godaddy.com";
Then I remove some unwanted characters from the input
$request->userDomain = str_replace('www.', '', $request->userDomain);
$request->userDomain = str_replace('http://', '', $request->userDomain);
$request->userDomain = str_replace('https://', '', $request->userDomain);
I put together the url for curl like this:
$url = $OTE_BASE_URL . "/v1/domains/available?domain=" . $request->userDomain;
Then I have the curl settings like this:
$header = array(
'Authorization: sso-key '.$API_KEY_OTE.':'.$API_SECRET_OTE.''
);
$ch = curl_init();
$timeout = 60;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
$checkDomain = json_decode($result, true);
Then i check the result with a series of if / else if statements:
if($checkDomain['available'] == 1 || $checkDomain['available'] == true) {
return redirect()->back()->with('successMsg', 'The domain name ' . $request->userDomain . ' is available! <a href="https://godaddy.com" target="_blank">Click here</a> to register it on Godaddy!');
} else if($checkDomain['available'] == '' || $checkDomain['available'] == 0 || $checkDomain['available'] == false) {
return redirect()->back()->with('failureMsg', 'Sorry! The domain name ' . $request->userDomain . ' is already registered! Try a different name.');
}
else if($checkDomain['code']) {
return redirect()->back()->with('failureMsg', $checkDomain['fields'][0]['message']);
} else {
return redirect()->back()->with('failureMsg', 'Please enter a valid domain name!');
}
This is a successful response body that comes from godaddy:
{
"available": true,
"currency": "USD",
"definitive": true,
"domain": "string",
"period": 0,
"price": 0
}
and this is an invalid response body:
{
"code": "string",
"fields": [
{
"code": "string",
"message": "string",
"path": "string",
"pathRelated": "string"
}
],
"message": "string"
}
CodePudding user response:
You must first check if it's error response or success response, only then try to use one of it's keys:
if (isset($checkDomain['available'])) {
if (!empty($checkDomain['available'])) {
return redirect()->back()->with('successMsg', 'The domain name ' . $request->userDomain . ' is available! <a href="https://godaddy.com" target="_blank">Click here</a> to register it on Godaddy!');
}
return redirect()->back()->with('failureMsg', 'Sorry! The domain name ' . $request->userDomain . ' is already registered! Try a different name.');
} elseif (isset($checkDomain['code'])) {
return redirect()->back()->with('failureMsg', $checkDomain['fields'][0]['message']);
}
return redirect()->back()->with('failureMsg', 'Please enter a valid domain name!');