Created
October 16, 2019 20:31
-
-
Save fdciabdul/f2b8c65764fd11020af079fda5e5d9fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Search { | |
public $doc = null; | |
public $results = []; | |
public $limit = 10; | |
public $URL = ""; | |
public $query = ""; | |
public function __construct($url) { | |
$this->doc = new DomDocument; | |
$this->URL = $url; | |
// We need to validate our document before refering to the id | |
$this->doc->validateOnParse = true; | |
} | |
public function response() { | |
return json_encode($this->results); | |
} | |
public function search($query) { | |
$this->query = urlencode($query); | |
return $this->load(); | |
} | |
public function setLimits($limit) { | |
$this->limit = $limit; | |
} | |
public function load() { | |
@$this->doc->loadHtml(file_get_contents($this->URL . $this->query)); | |
return new DOMXPath($this->doc); | |
} | |
public function searchClass($dom, $classname, $level="", $parent = null) { | |
return $dom->query(".//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]" . $level, $parent); | |
} | |
} | |
class QuoraSearch extends Search { | |
public $URL = 'https://id.quora.com/search?q='; | |
public function __construct() { | |
parent::__construct($this->URL); | |
} | |
public function process($query) { | |
$dom = $this->search($query); | |
$titles = $this->searchClass($dom, "question_link"); | |
$descriptions = $this->searchClass($dom, "row content"); | |
for($item = 0; $item < $titles->length && $item < $this->limit && $item < $descriptions->length; $item++) { | |
$title = $titles->item($item); | |
$desc = $descriptions->item($item); | |
$content_list = $this->searchClass($dom, "truncated_q_text", "", $desc); | |
$node = []; | |
$url = $title->getAttribute("href"); | |
$node['url'] = $url; | |
$node['title'] = $title->textContent; | |
$node['description'] = $content_list->length > 0 ? $content_list[0]->textContent: ""; | |
$this->results[] = $node; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment