This is the best thing ever. I just used it to scrape a website and it is fantastic, and saved me tons of time. It allows you to use jQuery style
selectors in PHP. This is an example where I’m scraping, but you could also change the page and add things before sending it out to the browser.
You install a PEAR package (you can also just copy the code, but this is the easiest way to keep it up to date):
pear channel-discover phpquery-pear.appspot.com
pear install phpquery/phpQuery
Then you include it in your code:
require('phpQuery.php');
Here’s an example of scraping form data from a CMS:
<?php
foreach($pages as $page_ID) {
$source_html = file_get_contents('http://example.com/cms/?pg_id=' . $page_ID);
$doc = phpQuery::newDocumentHTML($source_html);
$data = (object)array();
$tags = array();
foreach(pq('input:checked') as $input) {
$name = $input->getAttribute('name');
$value = $input->getAttribute('value');
$tags[] = $value;
}
foreach(pq('select') as $input) {
$name = $input->getAttribute('name');
$data->$name = $doc->find('select[name=' . $name . '] option:selected')->text();
}
foreach(pq('input') as $input) {
$name = $input->getAttribute('name');
$value = $input->getAttribute('value');
$type = $input->getAttribute('type');
if($type != 'checkbox') $data->$name = $value;
}
print_r($data);
print_r($tags);
unset($data, $tags);
}