1. Go to this page and download the library: Download duzun/hquery library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
duzun / hquery example snippets
// Optionally use namespaces
use duzun\hQuery;
// Either use composer, or a writable folder
// If not set, hQuery::fromURL() would make a new request on each call
hQuery::$cache_path = "/path/to/cache";
// Time to keep request data in cache, seconds
// A value of 0 disables cache
hQuery::$cache_expires = 3600; // default one hour
$doc = hQuery::fromHTML('<html><head><title>Sample HTML Doc</title><body>Contents...</body></html>');
// Set base_url, in case the document is loaded from local source.
// Note: The base_url property is used to retrieve absolute URLs from relative ones.
$doc->base_url = 'http://desired-host.net/path';
use duzun\hQuery;
// GET the document
$doc = hQuery::fromUrl('http://example.com/someDoc.html', ['Accept' => 'text/html,application/xhtml+xml;q=0.9,*/*;q=0.8']);
var_dump($doc->headers); // See response headers
var_dump(hQuery::$last_http_result); // See response details of last request
// with POST
$doc = hQuery::fromUrl(
'http://example.com/someDoc.html', // url
['Accept' => 'text/html,application/xhtml+xml;q=0.9,*/*;q=0.8'], // headers
['username' => 'Me', 'fullname' => 'Just Me'], // request body - could be a string as well
['method' => 'POST', 'timeout' => 7, 'redirect' => 7, 'decode' => 'gzip'] // options
);
use duzun\hQuery;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
$client = HttpClientDiscovery::find();
$messageFactory = MessageFactoryDiscovery::find();
$request = $messageFactory->createRequest(
'GET',
'http://example.com/someDoc.html',
['Accept' => 'text/html,application/xhtml+xml;q=0.9,*/*;q=0.8']
);
$response = $client->sendRequest($request);
$doc = hQuery::fromHTML($response, $request->getUri());
// Find all banners (images inside anchors)
$banners = $doc->find('a[href] > img[src]:parent');
// Extract links and images
$links = array();
$images = array();
$titles = array();
// If the result of find() is not empty
// $banners is a collection of elements (hQuery\Element)
if ( $banners ) {
// Iterate over the result
foreach($banners as $pos => $a) {
// $a->href property is the resolved $a->attr('href') relative to the
// documents <base href=...>, if present, or $doc->baseURL.
$links[$pos] = $a->href; // get absolute URL from href property
$titles[$pos] = trim($a->text()); // strip all HTML tags and leave just text
// Filter the result
if ( !$a->hasClass('logo') ) {
// $a->style property is the parsed $a->attr('style'), same as $a->attr('style', true)
if ( strtolower($a->style['position']) == 'fixed' ) continue;
$img = $a->find('img')[0]; // ArrayAccess
if ( $img ) $images[$pos] = $img->src; // short for $img->attr('src', true)
}
}
// If at least one element has the class .home
if ( $banners->hasClass('home') ) {
echo 'There is .home button!', PHP_EOL;
// ArrayAccess for elements and properties.
if ( $banners[0]['href'] == '/' ) {
echo 'And it is the first one!';
}
}
}
// Read charset of the original document (internally it is converted to UTF-8)
$charset = $doc->charset;
// Get the size of the document ( strlen($html) )
$size = $doc->size;
// The URL at which the document was requested
$requestUri = $doc->href;
// <base href=...>, if present, or the origin + dir path part from $doc->href.
// The .href and .src props are resolved using this value.
$baseURL = $doc->baseURL;
sh
composer
sh
cd hQuery.php/examples
php -S localhost:8000
# open browser http://localhost:8000/
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.