getWordUrl($word), true); } private function getWordUrl($word) { $word = trim($word); return self::URL . 'dictionary/definition.ajax?search=' . $word . '&lang=en'; } private function getWordExamplesUrl($word, $maxResult = 10, $offset = 0) { $word = trim($word); return sprintf('%sexamples.json?query=%s&maxResults=%d&startOffset=%d', self::EXAMPLES_URL, $word, $maxResult, $offset); } public function getWord($word) { $document = $this->getWordData($word); $wordData = new \stdClass(); $wordPageSelector = $document->first('.wordPage'); $wordData->lang = $wordPageSelector->attr('data-lang'); $wordData->word = $wordPageSelector->attr('data-word'); $wordData->next = $wordPageSelector->attr('data-next'); $wordData->prev = $wordPageSelector->attr('data-prev'); $wordData->short = trim($document->first('p.short')->text()); $wordData->long = trim($document->first('p.long')->text()); $wordData->instances = []; foreach ($document->find('.ordinal ') as $ordinal) { $definition = []; $definition['type'] = $ordinal->first('h3')->first('a')->attr('title'); $definition['definition'] = trim($ordinal->first('//h3[@class="definition"][1]/text()[2]', Query::TYPE_XPATH)); $definition['example'] = ($example = $ordinal->first('//div[@class="example"]', Query::TYPE_XPATH)) ? $example->text() : ''; foreach ($ordinal->find('.instances') as $instance) { $in = []; $in['type'] = str_replace(' ', '_', trim(strtolower($instance->first('dt::text()')), ': ')); $in['words'] = $instance->find('dd a::text()'); $in['definition'] = $instance->first('div.definition::text()'); $definition[$in['type']] = $in; } $wordData->instances[] = $definition; } $wordData->examples = $this->getExamples($word, 3); return $wordData; } private function getRandomWordUrl() { return self::URL . 'randomword.json'; } public function getRandomWord() { $data = json_decode(file_get_contents($this->getRandomWordUrl())); if ($data->status != 0) { throw new Exception('error'); } return $data->result->word; } public function getExamples($word, $maxResult = 10, $offset = 0) { return json_decode(file_get_contents($this->getWordExamplesUrl($word, $maxResult, $offset)), 1); } }