PHPHtmlParser\Dom::load PHP Method

load() public method

Attempts to load the dom from any resource, string, file, or URL.
public load ( string $str, array $options = [] )
$str string
$options array
    public function load($str, $options = [])
    {
        // check if it's a file
        if (strpos($str, "\n") === false && is_file($str)) {
            return $this->loadFromFile($str, $options);
        }
        // check if it's a url
        if (preg_match("/^https?:\\/\\//i", $str)) {
            return $this->loadFromUrl($str, $options);
        }
        return $this->loadStr($str, $options);
    }

Usage Example

Example #1
0
 /**
  * Gets departures from the given station starting at the given time.
  *
  * @param int $stationID
  * @param Carbon $time
  * @return array
  * @throws ApiException
  */
 public static function getDepartures(int $stationID, Carbon $time, int $maxJourneys = 10)
 {
     // prepare parameters for our request
     $query = ['input' => $stationID, 'boardType' => 'dep', 'time' => $time->format('H:i'), 'date' => $time->format('d.m.y'), 'maxJourneys' => $maxJourneys, 'start' => 'yes'];
     // send it to the bvg mobile site
     $response = \Requests::get(self::getApiEndpoint() . '?' . http_build_query($query));
     if ($response->status_code == 200) {
         // our results array
         $departures = [];
         // prepare document
         $dom = new Dom();
         $dom->load($response->body);
         // get date from API
         $date = $dom->find('#ivu_overview_input');
         $date = trim(substr($date->text, strpos($date->text, ':') + 1));
         $date = Carbon::createFromFormat('d.m.y', $date, 'Europe/Berlin');
         // get table data without the first line (header)
         $rows = $dom->find('.ivu_result_box .ivu_table tbody tr');
         // loop through each departure in the table
         foreach ($rows as $row) {
             // get columns
             $columns = $row->find('td');
             // explode time into two parts
             $time = explode(':', strip_tags($columns[0]));
             // push the departure onto our results array
             $departures[] = ['time' => $date->copy()->hour($time[0])->minute($time[1])->second(0), 'line' => trim(strip_tags($columns[1]->find('a')[0])), 'direction' => trim(strip_tags($columns[2]))];
         }
         // return results
         return $departures;
     } else {
         throw new ApiException('Failed getting station data from BVG API');
     }
 }
All Usage Examples Of PHPHtmlParser\Dom::load