· 5 years ago · Oct 09, 2020, 09:50 PM
1<?php
2class PhoneVerification {
3 /**
4 * Service License Key.
5 * @var string
6 */
7 private $key = "ukIXR7sz3YyTAWw1wjRdJyG6mWDQ69tE";
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;
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 // Create cURL Resouce/Object
74 $this->curl = curl_init();
75
76 // Set cURL options
77 curl_setopt($this->curl, CURLOPT_URL, $this->url);
78 curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
79 curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
80 curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
81
82 // Execute cURL instance.
83 $this->json = curl_exec($this->curl);
84 curl_close($this->curl);
85
86 // Convert JSON into Array
87 $this->result = json_decode($this->json, true);
88 }
89
90 /**
91 * Returns true if an error occured during initalization.
92 * @return bool
93 */
94 function init_error() {
95 if (isset($this->result['success']) &&
96 $this->result['success'] === true) {
97 return false;
98 } else {
99 return true;
100 }
101 }
102
103 /**
104 * Returns true if phone number is valid
105 * @return bool
106 */
107 function isValid() {
108 if ($this->result['valid'] === true) {
109 return true;
110 } else {
111 return false;
112 }
113 }
114
115 /**
116 * Returns true if phone number is malicious
117 * @return bool
118 */
119 function isMalicious() {
120 if ($this->result['valid'] === false ||
121 $this->result['recent_abuse'] === true) {
122 return true;
123 } else {
124 return false;
125 }
126 }
127}
128?>
129