Author Topic: Verify Phone Numbers using online API!  (Read 3366 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Verify Phone Numbers using online API!
« on: July 20, 2020, 06:44:16 pm »
Below is some code you can use to validate a phone number in the format of +18122822444 or +1812-282-2444 or +1(812)282-2444 and I'm sure there are more variations that are acceptable. Anyways, here is a way you can validate someone's phone number using numverify API! Make an account and get a key to create the request URL. Requests are HTTP only for free but if you pay you can use HTTPS. Again, not posting my API key in the forums so grab your free one and give it a whirl. I should note that the response JSON file actually also returns carrier and approximate city location of the registered phone. I just decided to not put that into my API function. Feel free to do it. It only requires a small tweak.

Code: QB64: [Select]
  1.  
  2. PRINT VerifyPhoneNumber("+1(812)282-2444")
  3.  
  4. FUNCTION VerifyPhoneNumber$ (phone AS STRING)
  5.     DIM URL AS STRING
  6.     DIM URLFile AS STRING
  7.     DIM a%
  8.     DIM Request AS STRING
  9.     URLFile = "validatephone"
  10.     URL = "http://apilayer.net/api/validate?access_key=YOURAPIKEYHERE&number=" + FormatAsHTTP(phone) + "&countrycode=&format=1"
  11.     a% = API_Request(URL, URLFile)
  12.     DIM U AS INTEGER
  13.     U = FREEFILE
  14.     OPEN URLFile FOR BINARY AS #U
  15.     IF LOF(U) <> 0 THEN
  16.         DO
  17.             LINE INPUT #U, Request
  18.         LOOP UNTIL INSTR(Request, "valid")
  19.     ELSE
  20.         CLOSE #U
  21.         KILL URLFile
  22.         VerifyPhoneNumber = ""
  23.         EXIT FUNCTION
  24.     END IF
  25.     CLOSE #U
  26.     KILL URLFile
  27.     IF INSTR(Request, "true") THEN
  28.         VerifyPhoneNumber = MID$(Request, 11, 4)
  29.     ELSE
  30.         VerifyPhoneNumber = MID$(Request, 11, 5)
  31.     END IF
  32.  
  33. FUNCTION FormatAsHTTP$ (Request AS STRING)
  34.     start% = 1
  35.     DO
  36.         position% = INSTR(start%, Request, " ")
  37.         IF position% THEN
  38.             MID$(Request, position%, 1) = "+"
  39.             start% = position% + 1
  40.         END IF
  41.     LOOP UNTIL position% = 0
  42.     FormatAsHTTP = Request
  43.  
  44.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  45.  
  46. FUNCTION API_Request (URL AS STRING, File AS STRING)
  47.     API_Request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
« Last Edit: July 20, 2020, 06:59:45 pm by SpriggsySpriggs »
Shuwatch!