Search::wikiSearch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
1
<?php
2
3
namespace Integrations\Connectors\Wikipedia;
4
5
use Log;
6
use App\Models\User;
7
8
class Search extends Wikipedia
9
{
10
    
11
    public function __construct()
12
    {
13
        
14
    }
15
    
16
    /**
17
     * Perform wiki search through MediaWiki action API.
18
     *
19
     * @param string $what The search string
20
     *
21
     * @return json object containing result if successful or
22
     *         error message & code if failed.
23
     */
24
    function wikiSearch($what)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
    {
26
        // Format: https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&generator=search&utf8=1&exsentences=1&exlimit=max&exintro=1&explaintext=1&gsrnamespace=0&gsrlimit=10&gsrsearch=SEARCH-TEXT
27
        $url = 'http://en.wikipedia.org/w/api.php';            
28
        $url .= '?action=query&format=json&prop=extracts&generator=search';
29
        $url .= '&utf8=1&exsentences=1&exlimit=max&exintro=1&explaintext=1';
30
        $url .= '&gsrnamespace=0&gsrlimit=10&gsrsearch=' . urlencode($what);
31
        
32
        $options = array(
33
            CURLOPT_RETURNTRANSFER => true,   // return web page
34
            CURLOPT_HEADER         => false,  // don't return headers
35
            CURLOPT_FOLLOWLOCATION => true,   // follow redirects
36
            CURLOPT_USERAGENT      => $_SERVER['HTTP_USER_AGENT'], // name of client
37
            CURLOPT_SSL_VERIFYPEER => false,
38
        ); 
39
        $ch = curl_init($url);
40
        curl_setopt_array($ch, $options);
41
        $response = curl_exec($ch);
42
        if ($response === false) {
43
            throw new Exception('Curl error: ' . curl_error($ch));
44
        }
45
        curl_close($ch);
46
        return $response;
47
    }
48
49
}
50