有可能通过使用Zend Request提取链接,标签内的字符串也可以从外部站点获取数据值并将所有这些复制到数组并回显它吗?
例如,在网站http://bills.ru/之后,从“событиянадолговомрынке”下面的表格中提取,所有数据都应存储在具有以下结构的数组中。
ID
日期
标题
URL
或者至少有人能给出一些实施Zend Request的好例子吗?
答案 0 :(得分:0)
我建议使用类似Goutte的内容,不允许您过滤返回的html。
如果您不想使用其他库,也可以使用Zend\Dom
查询Request
中的html。
答案 1 :(得分:0)
这是我设法在以下任务上完成的代码,它可以正常工作。
<?php
use Zend\Http\Client;
use Zend\Dom\Query;
/**
* Extracts date values, titles and links from block "события на долговом рынке" then save all date in 1
* array and prints it.
*
* Using Zend\Http\Client to make connection to website and further manipulate with Zend\Dom\Query's CSS selectors
* to retrieve date values, link and titles within block "событья на долговом рынке". Three private method are used to
* return values for each type and 1 public function used for retrieving
*
* @var client is Zend\Http\Client object and makes connection using function setUri() with declared website
* @var response servers as getting response from requested website
* @var dom is a Zend\Dom\Query object that allows manipulating with Zend\Http\Client objects
* @var results is a Zend\Dom\NodeList object made by using function execute()
* @var result used in foreach loop and for retrieving titles and url from a tag
* @var results_date same as @var results but for date values
* @var result_date same as @var result but for date values
* @var dateArray array where date values will be stored
* @var valuesArray array where data will be stored and printed afterwards
* @var html used to story content from @var client
*/
class BILLS
{
public $client;
public $response;
public $dom;
public $results;
public $result;
public $results_date;
public $result_date;
public $dateArray;
public $valuesArray;
public $html;
/**
* When new object with following class is created an object Zend\Http\Client is created and set Uri attribute.
* A request is being done to this object and data is put into $html variable for further use.
* @see client, response, html
*/
function __construct ()
{
$this->client = new \Zend\Http\Client();
$this->client->setUri('http://bills.ru');
$this->client->send();
$this->response = $this->client->getResponse();
$this->html = $this->response->getBody();
}
/**
* Returns date values within object
* @see result_date
*/
private function _date()
{
return $this->result_date->textContent;
}
/**
* Returns text content within object
* @see result
*/
private function _title()
{
return $this->result->textContent;
}
/**
* Returns url within object
* @see result
*/
private function _url()
{
return $this->result->getAttribute('href');
}
/**
* If connection has no problems a new Query object is created and searched for a tags with class new. Then
* using a foreach loop found data is stored in array and printed to screen. Uses 3 private function for returning
* values for each type that will be stored in array an printed afterwards.
*
* @see dom, results_date, dateArray, results, valuesArray, _date(), _url(), _title()
*
*/
public function printTask()
{
$iteration = 0;
$iterationData = 0;
if($this->response->getStatusCode() == 200)
{
$this->dom = new Query($this->html);
$this->results_date = $this->dom->execute('table tr td.news');
foreach ($this->results_date as $this->result_date)
{
if($iterationData < 5)
{
$dateArray[$iterationData] = $this->_date();
$iterationData++;
}
}
$this->results = $this->dom->execute('table tr td a.news');
foreach ($this->results as $this->result)
{
if($iteration < 5)
{
$valuesArray = array(
'id' => $iteration+1,
'date' => $dateArray[$iteration],
'title' => $this->_title(),
'url' => "http://bills.ru".$this->_url()
);
echo '<pre>';
print_r($valuesArray);
echo '</pre>';
$iteration++;
}
}
}
}
}
$object = new BILLS;
$object->printTask();
?>