PHPackages                             zround/sphinxapi - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. zround/sphinxapi

ActiveLibrary[Search &amp; Filtering](/categories/search)

zround/sphinxapi
================

Sphinx searchd client using SphinxQL (MySQL protocol) instead of deprecated native binary protocol

v0.8.2(1mo ago)15MITPHPPHP &gt;=7.0

Since May 8Pushed 1mo agoCompare

[ Source](https://github.com/zzround/sphinxql-sphinxapi)[ Packagist](https://packagist.org/packages/zround/sphinxapi)[ RSS](/packages/zround-sphinxapi/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (2)Used By (0)

SphinxClient PHP API (SphinxQL)
===============================

[](#sphinxclient-php-api-sphinxql)

基于 SphinxQL（MySQL 协议）的 PHP Sphinx searchd 客户端，兼容原始 `sphinxapi.php` 的全部公共方法签名和返回值格式，可直接替换原有调用代码。

A PHP Sphinx searchd client based on SphinxQL (MySQL protocol), fully compatible with the original `sphinxapi.php` public method signatures and return value formats — drop-in replacement.

背景 / Background
---------------

[](#背景--background)

Sphinx 官方已推荐使用 SphinxQL（TCP 9306 端口，MySQL 协议）替代原生二进制协议（TCP 9312 端口）。本项目将 `SphinxClient` 的内部实现从 `fsockopen` + 二进制打包改为 PDO + SphinxQL，上层代码无需任何修改。

Sphinx officially recommends SphinxQL (TCP port 9306, MySQL protocol) over the native binary protocol (TCP port 9312). This project rewrites `SphinxClient` internals from `fsockopen` + binary packing to PDO + SphinxQL, requiring no changes to upper-layer code.

要求 / Requirements
-----------------

[](#要求--requirements)

- PHP 7.0+（需启用 PDO + pdo\_mysql 扩展 / with PDO and pdo\_mysql extensions enabled）
- Sphinx/Manticore Search 开启 SphinxQL 监听（默认端口 9306 / with SphinxQL enabled, default port 9306）

安装 / Installation
-----------------

[](#安装--installation)

```
composer require zround/sphinxapi
```

使用方式 / Usage
------------

[](#使用方式--usage)

```
require_once 'vendor/autoload.php';

use Zround\SphinxClient;

$cl = new SphinxClient();
$cl->SetServer('127.0.0.1', 9306);
$cl->SetConnectTimeout(3);

// Ping
if ($cl->Ping()) {
    echo "OK\n";
}

// 查询 / Query
$cl->SetLimits(0, 20);
$cl->SetArrayResult(true);
$result = $cl->Query('关键词', '索引名');

print_r($result);
```

API 方法 / API Methods
--------------------

[](#api-方法--api-methods)

### 连接 / Connection

[](#连接--connection)

方法 / Method说明 / Description`SetServer($host, $port)`设置服务地址，默认端口 9306 / Set server address, default port 9306`SetConnectTimeout($timeout)`连接超时（秒）/ Connection timeout (seconds)`Open()`保持持久连接 / Open persistent connection`Close()`关闭持久连接 / Close persistent connection`Ping()`检测服务是否存活 / Check if server is alive### 查询参数 / Query Parameters

[](#查询参数--query-parameters)

方法 / Method说明 / Description`SetLimits($offset, $limit, $max, $cutoff)`分页与截断 / Pagination and cutoff`SetMaxQueryTime($ms)`最大查询时间 / Max query time`SetRankingMode($ranker, $rankexpr)`排名模式 / Ranking mode`SetSortMode($mode, $sortby)`排序模式 / Sort mode`SetWeights($weights)`字段权重（旧接口）/ Field weights (deprecated)`SetFieldWeights($weights)`字段权重 / Field weights`SetIndexWeights($weights)`索引权重 / Index weights`SetIDRange($min, $max)`文档 ID 范围 / Document ID range`SetFilter($attr, $values, $exclude)`整数过滤 / Integer filter`SetFilterRange($attr, $min, $max, $exclude)`范围过滤 / Range filter`SetFilterFloatRange($attr, $min, $max, $exclude)`浮点范围过滤 / Float range filter`SetFilterString($attr, $value, $exclude)`字符串过滤 / String filter`SetFilterStringList($attr, $value, $exclude)`字符串列表过滤 / String list filter`SetGroupBy($attr, $func, $groupsort)`分组 / Grouping`SetGroupDistinct($attr)`去重计数 / Distinct count`SetRetries($count, $delay)`重试 / Retries`SetArrayResult($on)`结果用数字索引 / Use numeric index for results`SetSelect($select)`自定义 SELECT 列 / Custom SELECT columns`SetQueryFlag($name, $value)`查询标志位 / Query flags`SetOuterSelect($orderby, $offset, $limit)`外层子查询 / Outer subquery`ResetFilters()`重置过滤 / Reset filters`ResetGroupBy()`重置分组 / Reset group-by`ResetQueryFlag()`重置查询标志 / Reset query flags`ResetOuterSelect()`重置外层查询 / Reset outer select### 执行查询 / Query Execution

[](#执行查询--query-execution)

方法 / Method说明 / Description`Query($query, $index, $comment)`单次查询 / Single query`AddQuery($query, $index, $comment)`添加到批量队列 / Add to batch queue`RunQueries()`执行批量队列 / Execute batch queue### 辅助方法 / Helper Methods

[](#辅助方法--helper-methods)

方法 / Method说明 / Description`BuildExcerpts($docs, $index, $words, $opts)`高亮摘要 / Build excerpts`BuildKeywords($query, $index, $hits)`关键词分词 / Tokenize keywords`EscapeString($string)`Sphinx 查询语法转义 / Escape Sphinx query syntax`UpdateAttributes($index, $attrs, $values, ...)`即时更新属性 / Update attributes in-place`Status($session)`服务状态 / Server status`FlushAttributes()`刷写属性到磁盘 / Flush attributes to disk`GetLastError()`最近错误 / Last error`GetLastWarning()`最近警告 / Last warning`IsConnectError()`是否连接错误 / Whether connection error与原版的差异 / Differences from Original
----------------------------------

[](#与原版的差异--differences-from-original)

项目 / Item说明 / Description默认端口 / Default port9312 → 9306批量查询 / Batch queries原版单次 TCP 发送全部查询，现改为逐条执行 / Originally sent all queries in one TCP call, now executes sequentiallyFlushAttributes无法返回 flush tag，返回 0 / Cannot return flush tag, returns 0SetTokenFilter无 SphinxQL 等价，调用时设警告忽略 / No SphinxQL equivalent, issues warning and ignoresSPH\_SORT\_TIME\_SEGMENTS用 INTERVAL() 近似，行为有差异 / Uses INTERVAL() approximation, behavior differs文件 / Files
----------

[](#文件--files)

- `src/SphinxClient.php` — Zround\\SphinxClient 类实现 / Class implementation

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance94

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity18

Early-stage or recently created project

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

32d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2c1ffbbb676d859076a1ebf3688445fad27f1bb757d1a4262773e73d9ca9d714?d=identicon)[zzround](/maintainers/zzround)

---

Tags

searchsphinxqlsphinxmanticore

### Embed Badge

![Health badge](/badges/zround-sphinxapi/health.svg)

```
[![Health](https://phpackages.com/badges/zround-sphinxapi/health.svg)](https://phpackages.com/packages/zround-sphinxapi)
```

###  Alternatives

[ripaclub/sphinxsearch

Sphinx Search library provides SphinxQL indexing and searching features

6032.3k3](/packages/ripaclub-sphinxsearch)[javer/sphinx-bundle

Provides integration of Sphinx search engine with Symfony using SphinxQL

24189.7k](/packages/javer-sphinx-bundle)[nilportugues/sphinx-search

Fully unit tested SphinxClient (SphinxAPI) for PHP5.3 and above to be used with SphinxSearch

25456.8k2](/packages/nilportugues-sphinx-search)[mnshankar/sphinxql

Integrate SphinxQL with Laravel 4. Simplifies Laravel-Sphinx(RT) search

103.7k](/packages/mnshankar-sphinxql)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
