A phpQuery-based php common collection class sharing _ php instance

Source: Internet
Author: User
This is a php collection class I wrote before. I have been using it all the time. I feel very simple and powerful. As long as I know a little about selector, I can collect any page and support https pages, simple collection is enough I. collection source code

The Code is as follows:


/**
* General List collection
* Version V1.3
* Author: JAE
*/
Require_once '../phpQuery. php ';
Class QueryList {

Private $ pageURL;
Private $ regArr = array ();
Public $ jsonArr = array ();
Private $ regRange;
Private $ html;
/*************************************** *********
* Parameter: page address selector array block Selector
* [Selector array] Description: Format array ("name" => array ("selector", "type "),.......)
* [Type] Description: Value "text", "html", "attribute"
* [Block selector]: selects several blocks according to the rules, and then selects the blocks separately.
**************************************** *********/
Function QueryList ($ pageURL, $ regArr = array (), $ regRange = '')
{
$ This-> pageURL = $ pageURL;

// 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: newjavasenthtml ($ 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;

}
}
// Reset the array pointer
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. Example

The Code is as follows:


Require 'query/QueryList. class. php ';


// Collect the OSC code sharing list, with the title linked to the author
$ Url = "http://www.oschina.net/code/list ";
$ Reg = array ("title" => array (". code_title a: eq (0) "," text ")," url "=> array (". code_title a: eq (0) "," href ")," author "=> array (" img "," title "));
$ Rang = ". code_list li ";
$ Hj = new QueryList ($ url, $ reg, $ rang );
$ Arr = $ hj-> jsonArr;
Print_r ($ arr );
// If you want to obtain the top 40 active contributor images on the right of the current page and obtain JSON data, you can write
$ Reg = array ("portrait" => array (". hot_top img", "src "));
$ Hj-> setQuery ($ reg );
$ Json = $ hj-> getJSON ();
Echo $ json ."

";

// Obtain the content on the OSC Content Page
$ 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 );

// Let's take so many examples. Is it very convenient to collect data?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.