First, collection class source code
Copy Code code as follows:
<?php
/**
* Universal List Collection Class
* Version V1.3
* Author: JAE
*/
Require_once '.. /phpquery/phpquery/phpquery.php ';
Class querylist{
Private $pageURL;
Private $REGARR = Array ();
Public $JSONARR = Array ();
Private $regRange;
Private $html;
/************************************************
* Parameters: Page Address selector array block selector
* "Selector array" description: Format Array ("Name" =>array ("selector", "type"),.......)
* "Type" description: Value "text", "HTML", "Properties"
* "Block selector": Refers to the first selection of a few chunks, and then separately in the block to make the relevant choice
*************************************************/
function Querylist ($pageURL, $regArr =array (), $regRange = ' ")
{
$this->pageurl = $pageURL;
In order to be able to obtain https://
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $this->pageurl);
curl_setopt ($ch, Curlopt_ssl_verifypeer, false);
curl_setopt ($ch, Curlopt_ssl_verifyhost, false);
curl_setopt ($ch, curlopt_returntransfer,1);
$this->html = curl_exec ($ch);
Curl_close ($ch);
if (!empty ($REGARR))
{
$this->regarr = $REGARR;
$this->regrange = $regRange;
$this->getlist ();
}
}
function Setquery ($regArr, $regRange = ')
{
$this->jsonarr=array ();
$this->regarr = $REGARR;
$this->regrange = $regRange;
$this->getlist ();
}
Private Function GetList ()
{
$hobj = phpquery::newdocumenthtml ($this->html);
if (!empty ($this->regrange))
{
$robj = PQ ($hobj)->find ($this->regrange);
$i = 0;
foreach ($robj as $item)
{
while (list ($key, $reg _value) =each ($this->regarr))
{
$iobj = PQ ($item)->find ($reg _value[0]);
Switch ($reg _value[1])
{
Case ' text ':
$this->jsonarr[$i] [$key] = Trim (PQ ($iobj)->text ());
Break
Case ' HTML ':
$this->jsonarr[$i] [$key] = Trim (PQ ($iobj)->html ());
Break
Default
$this->jsonarr[$i] [$key] = PQ ($iobj)->attr ($reg _value[1]);
Break
}
}
Resetting array pointers
Reset ($this->regarr);
$i + +;
}
}
Else
{
while (list ($key, $reg _value) =each ($this->regarr))
{
$lobj = PQ ($hobj)->find ($reg _value[0]);
$i = 0;
foreach ($lobj as $item)
{
Switch ($reg _value[1])
{
Case ' text ':
$this->jsonarr[$i ++][$key] = Trim (PQ ($item)->text ());
Break
Case ' HTML ':
$this->jsonarr[$i ++][$key] = Trim (PQ ($item)->html ());
Break
Default
$this->jsonarr[$i ++][$key] = PQ ($item)->attr ($reg _value[1));
Break
}
}
}
}
}
function Getjson ()
{
Return Json_encode ($this->jsonarr);
}
}
Ii. Examples of Use
Copy Code code as follows:
<?php
Require ' query/querylist.class.php ';
Collect the Code share list for the OSC, title link author
$url = "Http://www.oschina.net/code/list";
$reg = Array (". Code_title a:eq (0)", "text"), "url" =>array (". Code_title a:eq (0)", "href"), "=>array" author "=>array" ("img", "title"));
$rang = ". Code_list li";
$HJ = new Querylist ($url, $reg, $rang);
$arr = $hj->jsonarr;
Print_r ($arr);
If you also want to pick up the TOP40 active contributor image to the right of the current page, get the JSON data, so write
$reg = Array ("Portrait" =>array (". Hot_top img", "src"));
$hj->setquery ($reg);
$json = $hj->getjson ();
Echo $json. "
Content page content of OSC
$url = "http://www.oschina.net/code/snippet_186288_23816";
$reg = Array ("title" =>array ("). Qtitle H1 "," text ")," Con "=>array (". Content "," html "));
$HJ = new Querylist ($url, $reg);
$arr = $hj->jsonarr;
Print_r ($arr);
So many examples, is not used to do the collection is very convenient