· 5 years ago · Oct 09, 2020, 09:48 PM
1<?php
2class PhoneVerification {
3 /**
4 * Service License Key.
5 * @var string
6 */
7 private $key = "********************";
8
9 /**
10 * Phone Number Verified.
11 * @var string
12 */
13 private $phone;
14
15 /**
16 * Parameters for the HTTP GET request to the API.
17 * @var array
18 */
19 private $parameters = array(
20 "country" => array('US', 'CA')
21 );
22
23 /**
24 * Formatted Parameters for URL Query.
25 * @var string
26 */
27 private $formatted_parameters;
28
29 /**
30 * URL for API
31 * @var string
32 */
33 private $url;
34
35 /**
36 * Maximum time after calling request to drop and time out the request.
37 * @var int
38 */
39 private $timeout = 5;
40
41 /**
42 * Initalized cURL instance.
43 * @var resource|false
44 */
45 private $curl = curl_init();
46
47 /**
48 * JSON response of cURL reqest
49 * @var string|bool
50 */
51 private $json;
52
53 /**
54 * Decoded JSON returned as an array
55 * @var mixed
56 */
57 private $result;
58
59 function __construct($number) {
60 $this->phone = $number;
61
62 // Build formatted parameters
63 $this->formatted_parameters = http_build_query($this->parameters);
64
65 // Build request URL
66 $this->url = sprintf(
67 'https://www.ipqualityscore.com/api/json/phone/%s/%s?%s',
68 $this->key,
69 $this->phone,
70 $this->formatted_parameters
71 );
72
73 // Set cURL options
74 curl_setopt($this->curl, CURLOPT_URL, $this->url);
75 curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
76 curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
77 curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
78
79 // Execute cURL instance.
80 $this->json = curl_exec($this->curl);
81 curl_close($this->curl);
82
83 // Convert JSON into Array
84 $this->result = json_decode($this->json, true);
85 }
86
87 /**
88 * Returns true if an error occured during initalization.
89 * @return bool
90 */
91 function init_error() {
92 if (isset($this->result['success']) &&
93 $this->result['success'] === true) {
94 return false;
95 } else {
96 return true;
97 }
98 }
99
100 /**
101 * Returns true if phone number is valid
102 * @return bool
103 */
104 function isValid() {
105 if ($this->result['valid'] === true) {
106 return true;
107 } else {
108 return false;
109 }
110 }
111
112 /**
113 * Returns true if phone number is malicious
114 * @return bool
115 */
116 function isMalicious() {
117 if ($this->result['valid'] === false ||
118 $this->result['recent_abuse'] === true) {
119 return true;
120 } else {
121 return false;
122 }
123 }
124}
125?>
126