Home > Enterprise >  Abstract Api Validation for Mobile number
Abstract Api Validation for Mobile number

Time:10-31

Hello everyone I am trying to validate the mobile number using abstract api validation but I am stuck to check which number is valid and which number is not valid for this I write a code.

$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://phonevalidation.abstractapi.com/v1/?api_key=my_api&phone=14152007986');
        $response = curl_exec($ch);
        curl_close($ch);
        $data = json_decode($response, true);
        $check = (string)$data;
        if (strpos($check, 'true') == true)
        {
            echo "PhoneNo is valid";
        }
        if (strpos($check, 'false') == false)
        {
            echo "PhoneNo is invalid";
        }

In the above code the phone number is correct as I am giving the phone number as example but still its showing me PhoneNo is invalid can any one help me to create a logic for it

CodePudding user response:

strpos — Find the position of the first occurrence of a substring in a string

So if the result is "true" string your validation with strpos should look like

if (strpos($check, 'true') !== false)
{
    echo "PhoneNo is valid";
} else
    echo "PhoneNo is invalid";
}

Because function returns false if the needle was not found.

CodePudding user response:

The Problem what i see is: You get a string then you parse the string into an array to cast it back into a string. so you can use your response right away. I don't know what the response looks like when it succeeds or fails. I only get api key missing.

curl_setopt($ch, CURLOPT_URL, 'https://phonevalidation.abstractapi.com/v1/?api_key=my_api&phone=14152007986');
$response = curl_exec($ch);
curl_close($ch);        

if (strpos($response, 'true') == true)
{
    echo "PhoneNo is valid";
} else {
    echo "PhoneNo is invalid";
}
  • Related