phpquery
Query PHP configuration and information
TLDR
List available PHP versions
List available SAPIs for PHP 7.3
List enabled extensions for PHP 7.3 with the cli SAPI
Check if the JSON extension is enabled for PHP 7.3 with the apache2 SAPI
SYNOPSIS
Since phpQuery is a PHP library, it is executed indirectly via the PHP interpreter. The general synopsis for using a PHP script that incorporates phpQuery from the command line is:
php [PHP_OPTIONS] your_script.php [SCRIPT_ARGUMENTS...]
Here, your_script.php is a custom PHP file that includes and uses the phpQuery library to perform its tasks.
PARAMETERS
-f
Executes the specified PHP
-r
Executes the given PHP string directly. Useful for quick one-liners involving phpQuery, typically requiring the library to be autoloaded or explicitly included.
--
Marks the end of PHP options and treats all subsequent arguments as script arguments. These are passed directly to your phpQuery-enabled script and can be accessed via the $argv array within the script.
Script-defined arguments
Any custom PHP script using phpQuery can define and parse its own command-line arguments (e.g., --input-file, --selector, --output-format). These are handled by the script's internal logic, not by phpQuery directly.
DESCRIPTION
phpQuery is a powerful PHP library that provides a fluent interface for DOM traversal and manipulation, highly inspired by the popular JavaScript library jQuery. Unlike a traditional Linux command-line tool, phpQuery is not a standalone executable. Instead, it is integrated into PHP scripts, allowing developers to programmatically interact with HTML and XML documents. Its primary use cases include web scraping, parsing HTML data, modifying document structures, and extracting specific content using familiar CSS selector syntax. When used from the command line, it involves executing a PHP script that utilizes the phpQuery library to process data piped into standard input, read from files, or fetched from URLs, providing a flexible way to automate web content processing tasks.
CAVEATS
- Library, Not Executable: phpQuery is a PHP library. There is no standalone phpquery binary to execute directly. Usage requires a PHP interpreter and a PHP script that incorporates the library.
- PHP Dependency: Requires a working PHP installation (typically PHP 5.3+, though newer versions might have compatibility issues without polyfills, as the library is not actively maintained for the latest PHP releases).
- Performance & Memory: Parsing very large HTML/XML documents can be memory-intensive, as phpQuery builds an in-memory DOM representation.
- Maintenance Status: While still functional, phpQuery is not as actively maintained as it once was. More modern PHP alternatives like Symfony DomCrawler or Goutte (which builds on DomCrawler) are often preferred for new projects due to ongoing development and better support for modern PHP versions.
<I>INSTALLATION</I>
phpQuery can be installed by downloading the library files and including them in your PHP project. The recommended method for modern PHP projects is via Composer, by adding "electrolinux/phpquery": "*" to your composer.json dependencies and then running composer install.
<I>BASIC COMMAND-LINE USAGE EXAMPLE</I>
To demonstrate phpQuery usage from the command line, consider a PHP script named parse_html.php:
<?php
require_once 'vendor/autoload.php'; // If using Composer
// Or: require_once 'phpQuery.php'; // Manual include
$html = file_get_contents('php://stdin'); // Read from stdin
$doc = phpQuery::newDocument($html);
// Find all <a> tags and print their href attributes
pq('a')->each(function($i, $el) {
echo pq($el)->attr('href') . "\n";
});
?>
You can then run this script from the command line like:
curl https://example.com | php parse_html.php
or
cat input.html | php parse_html.php
This example parses HTML from standard input and extracts all link href attributes.
HISTORY
phpQuery was initially developed around 2008-2009, drawing significant inspiration from the hugely successful jQuery JavaScript library. Its goal was to bring jQuery's elegant and powerful DOM manipulation capabilities to the PHP environment, making it much easier for PHP developers to parse, traverse, and modify HTML/XML documents. It quickly gained popularity in the PHP community, particularly for web scraping and content manipulation tasks, due to its intuitive CSS selector syntax and chainable methods. While it played a crucial role in its time, its development slowed down in the mid-2010s, with newer, more actively maintained libraries emerging as preferred alternatives for modern PHP applications.