Search code examples
phpapiabstract

Abstract Api Validation for Mobile number


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


Solution

  • 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.