ZapMachine/resource/Brave.class.php

77 lines
2 KiB
PHP
Raw Permalink Normal View History

<?php
class Brave implements Search {
private $key = BRAVE_API_KEY;
private $base_url = BRAVE_BASE;
private $q_prefix = '?q=';
private $count_prefix = '&count=';
private $adultPrefix = '&safesearch=';
private $word;
private $start;
private $size;
private $myCurl;
public function __construct() {
if (ZAP_ADULT == true) {
$this->adultPrefix .= 'off';
} else {
$this->adultPrefix .= 'strict';
}
}
public function setQuery($word) {
$this->word = $word;
2026-07-17 12:34:37 +02:00
}
2026-07-17 12:34:37 +02:00
public function getData() {
$this->buildRequest();
$return_array = $this->prepareData();
return $return_array;
}
/**
* Build the request URL and cURL handle after all setParam() calls have been made.
* This ensures start/offset and size are available when constructing the URL.
*/
private function buildRequest() {
$request = $this->q_prefix . urlencode($this->word);
if (isset($this->start)) {
$request .= '&offset=' . $this->start;
}
$request .= $this->count_prefix . (isset($this->size) ? (int)$this->size : 3);
$request .= $this->adultPrefix;
$this->myCurl = new ZAPCurl($this->base_url . $request, false);
$this->myCurl->setHeader('X-Subscription-Token', $this->key);
$this->myCurl->setHeader('Accept', 'application/json');
}
public function setParam($key, $value) {
$this->$key = $value;
}
/**
* Prepare a project wide universal array with results
*/
private function prepareData() {
$json = $this->myCurl->fetchCurlData();
$se_data = json_decode($json, true);
if (!isset($se_data['results'][0]['properties']['url'])) {
$data['result'] = false;
return $data;
}
$data['result'] = true;
$data['engine'] = __CLASS__;
$i = 0;
foreach($se_data['results'] as $result) {
$data['results'][$i]['clickurl'] = $result['properties']['url'];
$data['results'][$i]['referer'] = isset($result['url']) ? $result['url'] : '';
$data['results'][$i]['thumb'] = isset($result['thumbnail']['src']) ? $result['thumbnail']['src'] : '';
$i++;
}
return $data;
}
}
?>