Repo eski ve artık kullanılmayan paketleri içeriyor. Fork edilip güncellenmesi lazım. Ben biraz düzenledim, şöyle kullanabilirsiniz:
Önce gereksinimleri kurun:
composer require guzzlehttp/guzzle symfony/css-selector symfony/dom-crawler
Sonra app içerisine Services isimli bir klasör oluşturup içerisine Point.php dosyası ekleyin:
app/Services/Point.php:
<?php
namespace App\Services;
use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;
/**
* Class Point
*
* @package Mews\Tsl
* @author Muharrem ERİN <me@mewebstudio.com>
* @licence MIT
*/
class Point
{
/**
* @var \GuzzleHttp\Client
*/
protected $client;
/**
* @var string
*/
protected $body;
/**
* @var \Symfony\Component\DomCrawler\Crawler
*/
protected $crawler;
/**
* @var array
*/
protected $cols = [];
/**
* @var array
*/
protected $rows = [];
/**
* @var string
*/
protected $url;
/**
* Point constructor.
*
* @param $url
* @param \GuzzleHttp\Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}
/**
* @param $url
*/
public function setUrl($url)
{
$this->url = $url;
$this->body = $this->client->get($this->url)->getBody()->getContents();
}
/**
* @param string $element
*/
public function parse($element)
{
$this->crawler = new Crawler($this->body);
$this->crawler->filter($element)->filter('tr')->each(function (Crawler $row, $i) {
$row->filter('td')->each(function (Crawler $col, $j) use ($i) {
if ($i == 0) {
$this->cols[] = $col->text();
} else {
if ($j == 0) {
$this->rows[$i][] = preg_replace('/' . $i . './', '', $col->text());
} else {
$this->rows[$i][] = $col->text();
}
}
});
});
}
/**
* @return array
*/
public function getCols()
{
return $this->cols;
}
/**
* @return array
*/
public function getRows()
{
return $this->rows;
}
/**
* @param null $id
* @param null $class
* @return string
*/
public function toTable($id = null, $class = null)
{
$html = '<table' . ($id ? ' id="' . $id . '"' : null) . ($class ? ' class="' . $class . '"' : null) . '>';
$html .= '<thead>';
$html .= '<tr>';
foreach ($this->getCols() as $col) {
$html .= '<th>' . $col . '</th>';
}
$html .= '</tr>';
$html .= '</thead>';
$html .= '<tbody>';
foreach ($this->getRows() as $row) {
$html .= '<tr>';
foreach ($row as $item) {
$html .= '<td>' . $item . '</td>';
}
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '<table>';
return $html;
}
}
Şu şekilde kullanırsınız:
public function show(\App\Services\Point $point)
{
$point->setUrl('http://www.tff.org/Default.aspx?pageId=198');
$point->parse('div#ctl00_MPane_m_198_1890_ctnr_m_198_1890_Panel1');
$table = $point->toTable();
return view('puan-durumu', compact('table'));
}
puan-durumu.blade.php:
{!! $table !!}