@@ -32,6 +32,37 @@ private function _fetchHtml($word)
3232 return $ response ;
3333 }
3434
35+ private function _request__KBBI_API_Zhirrr ($ word )
36+ {
37+ $ encodedWord = rawurlencode ($ word );
38+ $ url = "https://kbbi-api-zhirrr.vercel.app/api/kbbi?text= " . $ encodedWord ;
39+ $ ch = curl_init ($ url );
40+
41+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
42+ curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
43+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , false ); // Disable SSL host verification
44+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false ); // Disable SSL peer verification
45+
46+ $ response = curl_exec ($ ch );
47+
48+ if (curl_errno ($ ch )) {
49+ throw new Exception ('Error: ' . curl_error ($ ch ));
50+ }
51+
52+ curl_close ($ ch );
53+
54+ // Decode JSON response ke array association
55+ $ result = json_decode ($ response , true );
56+
57+ // Periksa apakah dekoding berhasil
58+ if (json_last_error () === JSON_ERROR_NONE ) {
59+ return $ result ;
60+ } else {
61+ // echo 'Error decoding JSON: ' . json_last_error_msg();
62+ return [];
63+ }
64+ }
65+
3566 private function _cleanText ($ text )
3667 {
3768 return preg_replace ('/\s+/ ' , ' ' , trim ($ text ));
@@ -215,51 +246,83 @@ private function _parserV3($htmlData, $word)
215246 return count ($ dataResponse ) ? $ dataResponse : [];
216247 }
217248
218- public function searchWord ($ word )
249+ private function _KBBI_official ($ word )
219250 {
220251 // Clean the word
221252 $ cleanWord = $ this ->_cleanWord ($ word );
222253
254+ // If not found in the database, fetch from the web
223255 $ htmlData = $ this ->_fetchHtml ($ word );
224256
225- $ dataResponse = [];
226-
227257 // parserV1 disabled because has been enhance in parserV3
228- /*$_parserV1 = $this->_parserV1($htmlData, $cleanWord, $wordType );
258+ /*$_parserV1 = $this->_parserV1($htmlData, $cleanWord);
229259 if(count($_parserV1)){
230260 $dataResponse = $_parserV1;
231261
232262 return $dataResponse;
233263 }*/
234264
235- $ _parserV2 = $ this ->_parserV2 ($ htmlData , $ cleanWord , $ wordType );
236- if (count ($ _parserV2 )){
237- $ dataResponse = $ _parserV2 ;
238-
239- return $ dataResponse ;
265+ $ _parserV2 = $ this ->_parserV2 ($ htmlData , $ cleanWord );
266+ if (count ($ _parserV2 ))
267+ {
268+ return $ _parserV2 ;
240269 }
241270
242- $ _parserV3 = $ this ->_parserV3 ($ htmlData , $ cleanWord , $ wordType );
243- if (count ($ _parserV3 )){
244- $ dataResponse = $ _parserV3 ;
245-
246- return $ dataResponse ;
271+ $ _parserV3 = $ this ->_parserV3 ($ htmlData , $ cleanWord );
272+ if (count ($ _parserV3 ))
273+ {
274+ return $ _parserV3 ;
247275 }
248-
249- return false ;
250276
251- /*$dataResponse = [];
277+ return [];
278+ }
252279
253- $_parserV1 = $this->_parserV1($htmlData, $cleanWord);
254- if(count($_parserV1)){
255- $dataResponse = $_parserV1;
256- } else {
257- $_parserV2 = $this->_parserV2($htmlData, $cleanWord);
258- if(count($_parserV2)){
259- $dataResponse = $_parserV2;
280+ private function _KBBI_byZhirrr ($ word ): array
281+ {
282+ // Clean the word
283+ $ cleanWord = $ this ->_cleanWord ($ word );
284+
285+ $ response = $ this ->_request__KBBI_API_Zhirrr ($ word );
286+ $ dataResponse = [];
287+
288+ if (count ($ response ))
289+ {
290+ $ arti = [];
291+ $ tesaurusLink = "http://tesaurus.kemdikbud.go.id/tematis/lema/ " . $ cleanWord ;
292+
293+ foreach ($ response ['arti ' ] as $ k => $ v ) {
294+ $ arti [$ k ]['deskripsi ' ] = $ v ;
260295 }
261- }
262296
263- return count($dataResponse) ? $dataResponse : false;*/
297+ $ dataResponse [] = [
298+ 'word ' => $ cleanWord ,
299+ 'lema ' => $ response ['lema ' ],
300+ 'arti ' => $ arti ,
301+ 'tesaurusLink ' => $ tesaurusLink ,
302+ ];
303+
304+ return $ dataResponse ;
305+ }
306+
307+ return $ dataResponse ;
308+ }
309+
310+ public function searchWord ($ word )
311+ {
312+ // OFFICIAL
313+ $ _KBBI_official = $ this ->_KBBI_official ($ word );
314+ if (count ($ _KBBI_official ))
315+ {
316+ return $ _KBBI_official ;
317+ }
318+
319+ // ZHIRRR
320+ $ _KBBI_byZhirrr = $ this ->_KBBI_byZhirrr ($ word );
321+ if (count ($ _KBBI_byZhirrr ))
322+ {
323+ return $ _KBBI_byZhirrr ;
324+ }
325+
326+ return false ;
264327 }
265328}
0 commit comments