{"id":15099163,"url":"https://github.com/chinahehex/hehep-hclient","last_synced_at":"2026-01-22T05:03:26.145Z","repository":{"id":240008928,"uuid":"801422611","full_name":"chinahehex/hehep-hclient","owner":"chinahehex","description":"hehep-hclient 是一个PHP客户端工具组件","archived":false,"fork":false,"pushed_at":"2025-06-18T12:41:52.000Z","size":408,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-21T01:51:30.882Z","etag":null,"topics":["client","curl","httpclient","php","socket"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chinahehex.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-16T07:47:21.000Z","updated_at":"2025-06-18T12:41:56.000Z","dependencies_parsed_at":"2024-05-16T09:03:17.709Z","dependency_job_id":"cca7bc6a-90d6-41e7-81fc-4c8406170d9e","html_url":"https://github.com/chinahehex/hehep-hclient","commit_stats":null,"previous_names":["chinahehex/hehep-hclient"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/chinahehex/hehep-hclient","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chinahehex%2Fhehep-hclient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chinahehex%2Fhehep-hclient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chinahehex%2Fhehep-hclient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chinahehex%2Fhehep-hclient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chinahehex","download_url":"https://codeload.github.com/chinahehex/hehep-hclient/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chinahehex%2Fhehep-hclient/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28655038,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-22T01:17:37.254Z","status":"online","status_checked_at":"2026-01-22T02:00:07.137Z","response_time":144,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["client","curl","httpclient","php","socket"],"created_at":"2024-09-25T17:06:50.289Z","updated_at":"2026-01-22T05:03:26.139Z","avatar_url":"https://github.com/chinahehex.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hehep-hclient\n\n## 介绍\n- hehep-hclient 是一个PHP客户端工具组件\n\n## 安装\n- **gitee下载**:\n```\ngit clone git@gitee.com:chinahehex/hehep-hclient.git\n```\n\n- **github下载**:\n```\ngit clone git@github.com:chinahehex/hehep-hclient.git\n```\n- 命令安装：\n```\ncomposer require hehex/hehep-hclient\n```\n\n## 组件配置\n- 基础配置\n```php\n\n$config = [\n    //'class'=\u003e'hclient\\Client',// 客户端管理器\n    // 默认传输器\n    'transport'=\u003e'curl',// 目前提供curl,socket,stream 三种传输方式,\n    \n    // request 对象格式对象\n    'formatters'=\u003e[\n        'json'=\u003e'json',\n        'xml'=\u003e'xml',\n        'none'=\u003e'none'\n    ],\n\n    // response 对象格式对象\n    'parsers'=\u003e[\n        'json'=\u003e'json',\n        'xml'=\u003e'xml',\n        'none'=\u003e'none'\n    ],\n    // 自定义站点信息\n    'sites'=\u003e[\n        \n        'api'=\u003e[\n            // 请求类相关属性\n            'class'=\u003e'hclient\\base\\Request',\n            'baseUrl'=\u003e'http://xxxx.xxxx.cn/',// 站点域名或地址\n            'method'=\u003e'POST',// http 方法\n            'protocol'=\u003e'',// 请求协议,比如http,一般配置socket使用\n            'format'=\u003e'json',// 格式化名称\n            'options'=\u003e[],// 传输协议参数\n            'response'=\u003e[\n                // 响应类相关属性\n                'class'=\u003e'hclient\\extend\\ApiResponse',\n                'protocol'=\u003e'',// 请求协议,比如http,一般配置socket使用\n                'format'=\u003e'json',// 格式化名称\n            ],\n        ],\n        'sina'=\u003e[\n            'baseUrl'=\u003e'http://blog.sina.com.cn/',\n            'method'=\u003e'GET'\n        ],\n        'baidu'=\u003e[\n            'baseUrl'=\u003e'http://www.thinkphp.cn/',\n            'method'=\u003e'GET'\n        ]\n    ],\n];\n\n```\n\n## 基本示例\n\u003e 普通请求　GET HTTP 请求,支持OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  \n```php\nuse hclient\\Client;\n$hclient = new Client();\n\n// 发送get 请求\n$html = $hclient-\u003eget(\"http://www.baidu.com/\")-\u003esend()-\u003egetContent();\n\n// 设置请求参数\n$html = $hclient-\u003eget(\"http://www.baidu.com/\",['id'=\u003e1])-\u003esend()-\u003egetContent();\n\n// 简写方式\n$html = $hclient-\u003egetResult(\"http://www.baidu.com/\",['id'=\u003e1]);\n\n// 发送post 请求\n$html = $hclient-\u003epost(\"http://www.baidu.com/\",['id'=\u003e1])-\u003esend()-\u003egetContent();\n// 简写方式\n$html = $hclient-\u003epostResult(\"http://www.baidu.com/\",['id'=\u003e1]);\n\n// 其他请求method\n$html = $hclient-\u003exxxResult(\"http://www.baidu.com/\",['id'=\u003e1]);\n\n```\n\n## api 请求\n\n### 配置\n```php\n$config = [\n    // 自定义站点信息\n    'sites'=\u003e[\n        'orderapi'=\u003e[\n            // 请求类相关属性\n            'class'=\u003e'hclient\\base\\Request',\n            'baseUrl'=\u003e'http://xxxx.xxxx.cn/',// 站点域名或地址\n            'method'=\u003e'POST',// http 方法\n            'protocol'=\u003e'',// 请求协议,比如http,一般配置socket使用\n            'format'=\u003e'',// 格式化名称,json\n            'response'=\u003e[\n                'class'=\u003e'hclient\\extend\\ApiResponse',// 定义响应类\n                'protocol'=\u003e'',// 请求协议,比如http,一般配置socket使用\n                'format'=\u003e'json',// 格式化名称,\n                'varCode'=\u003e'code',// 业务状态码字段名称,\n                'varMsg'=\u003e'message',// 业务提示信息字段名称,\n                'varResult'=\u003e'data',//  业务数据字段名称,\n                'succCode'=\u003e200,// 业务状态码成功值,\n            ],\n        ],\n    ],\n];\n\n```\n\n### 基本示例\n```php\nuse hclient\\Client;\n$hclient = new Client();\n$response = $hclient-\u003eapi('orderapi','product/add',['name'=\u003e'商品','price'=\u003e10.2,'stock'=\u003e100]);\n$response = $hclient-\u003esite('orderapi')-\u003eapi('product/add',['name'=\u003e'商品','price'=\u003e10.2,'stock'=\u003e100]);\n$response = $hclient-\u003eorderapi-\u003eapi('product/add',['name'=\u003e'商品','price'=\u003e10.2,'stock'=\u003e100]);\n\n// 判断请求是否成功,业务状态码是否正确\n$response-\u003eisOk();\n\n// 自定义状态码\n$response-\u003eisOk(2000);\n\n// 获取业务提示消息\n$response-\u003egetMessage();\n\n// 获取业务数据\n$response-\u003egetResult();\n\n// 获取业务状态码\n$response-\u003egetErrorCode();\n\n```\n\n## 站点请求\n\n### 添加站点\n```php\nuse hclient\\Client;\n$hclient = new Client();\n\n// 添加站点\n$hclient-\u003eaddSite('sina',[\n    'baseUrl'=\u003e'https://sports.sina.com.cn/',\n    'method'=\u003e'GET'\n]);\n\n$response = $hclient-\u003euri(\"sina\",\"system/site/siteInit\",['id'=\u003e1])-\u003esend();\n// 获取返回的原始内容\n$content = $response-\u003egetContent();\n\n// 获取格式化后的内容\n$data = $response-\u003esetFormat(\"json\")-\u003egetData();\n\n// 直接获取格式化后数据\n$data = $hclient-\u003euriResult(\"sina\",\"system/site/siteInit\",['id'=\u003e1]);\n\n```\n\n### post,get请求\n```php\nuse hclient\\Client;\n$hclient = new Client();\n$hclient-\u003esite('baidu')-\u003eget('system/site/siteInit');\n$hclient-\u003esite('baidu')-\u003epost('system/site/siteInit',['id'=\u003e1]);\n$hclient-\u003esite('baidu')-\u003egetResult('system/site/siteInit');\n$hclient-\u003esite('baidu')-\u003epostResult('system/site/siteInit',['id'=\u003e1]);\n```\n\n### 创建预定义站点\n```php\nuse hclient\\Client;\n$hclient = new Client();\n$response = $hclient-\u003esite('baidu')\n    -\u003euri('system/site/siteInit',['id'=\u003e1])\n    -\u003esend();\n\n```\n\n### 创建全新站点\n```php\nuse hclient\\Client;\n$hclient = new Client();\n$response = $hclient-\u003esite()\n    -\u003esetBaseUrl('https://sports.sina.com.cn/')\n    -\u003euri('system/site/siteInit',['id'=\u003e1])\n    -\u003esend();\n\n```\n\n## 批量请求\n\n### 基础示例\n```php\nuse hclient\\Client;\n$hclient = new Client();\n\n// 方式1\n$reqeusts = [\n    'reqeust1'=\u003e$hclient-\u003euri(\"md\",\"system/site/siteInit\",['id'=\u003e1]),\n    'reqeust2'=\u003e$hclient-\u003euri(\"md\",\"system/site/siteInit\",['id'=\u003e2]),\n];\n\n$responses = $hclient-\u003ebatchSend($reqeusts);\n$content = $responses['reqeust1']-\u003egetContent();\n$content = $responses['reqeust2']-\u003egetContent();\n\n\n// 方式2\n$reqeusts = [\n    $hclient-\u003euri(\"md\",\"system/site/siteInit\",['id'=\u003e1])-\u003esetIndex('reqeust1'),\n    $hclient-\u003euri(\"md\",\"system/site/siteInit\",['id'=\u003e2])-\u003esetIndex('reqeust2'),\n];\n\n$responses = $hclient-\u003ebatchSend($reqeusts);\n$content = $responses['reqeust1']-\u003egetContent();\n$content = $responses['reqeust2']-\u003egetContent();\n\n```\n\n### 创建请求组\n```php\nuse hclient\\Client;\n$hclient = new Client();\n\n$requestGroup = $hclient-\u003ebatch();\n$requestGroup-\u003egetResult('http://www.baidu.com')-\u003esetIndex(\"reqeust1\");\n$requestGroup-\u003egetResult('http://www.baidu.com')-\u003esetIndex(\"reqeust2\");\n$res_result = $requestGroup-\u003esend();\n$res_result['reqeust1'];\n$res_result['reqeust2'];\n\n$requestGroup = $hclient-\u003ebatch();\n$requestGroup-\u003eget('http://www.baidu.com')-\u003esetIndex(\"reqeust1\");\n$requestGroup-\u003egetResult('http://www.baidu.com')-\u003esetIndex(\"reqeust2\");\n$res_result = $requestGroup-\u003esend();\t\n$res_result['reqeust1'];\n$res_result['reqeust2'];\n\n// reqeust1 返回的结果是response 对象\n$response = $res_result['reqeust1'];\n$html = $res_result['reqeust2'];\n\n```\n\n## 设置参数\n\n### 设置格式化\n```\n目前支持三种数据格式化,分别为\njson,xml,none(无)\n```\n- 注册格式化\n```php\nuse hclient\\Client;\n$hclient = new Client();\n$hclient-\u003eregisterFormat('json','hclient\\formatters\\JsonFormatter');\n$hclient-\u003eregisterParser('json','hclient\\formatters\\JsonParser');\n\n```\n\n- 设置请求格式化\n```php\nuse hclient\\Client;\n$hclient = new Client();\n// $request_data json 编码后传输\n$request_data = ['id'=\u003e1];\n$response = $hclient-\u003epost('http://www.baidu.com',$request_data)-\u003esetFormat('json')-\u003esend();\n\n// 对返回结果json 解码\n$data = $response-\u003esetFormat('json')-\u003egetData();\n\n```\n\n\n\n### 设置头部信息\n- 设置headers信息\n```php\nuse hclient\\Client;\n$hclient = new Client();\n$reqeust = $hclient-\u003euri(\"md\",\"system/site/siteInit\",['id'=\u003e1]);\n$reqeust-\u003eaddHeaders([\n    'c'=\u003e1,\n    'n'=\u003etime(),\n]);\n\n```\n\n- 设置cookies\n```php\nuse hclient\\Client;\n$hclient = new Client();\n$reqeust = $hclient-\u003euri(\"md\",\"system/site/siteInit\",['id'=\u003e1]);\n$reqeust-\u003eaddCookie([\n    \"name\"=\u003e\"ok\",// cookie 名称\n    'value'=\u003e\"12121\",// cookie 值\n    \"expire\"=\u003e60 * 30,// cookie 有效期\n]);\n\n$reqeust-\u003esetCookie(\"ok\",\"value\",60 * 30);\n\n```\n\n- 设置Method\n```php\nuse hclient\\Client;\n$hclient = new Client();\n$reqeust = $hclient-\u003euri(\"md\",\"system/site/siteInit\",['id'=\u003e1]);\n\n// 设置http method\n$reqeust-\u003esetMethod(\"post\");\n$reqeust-\u003esetMethod(\"get\");\n\n```\n\n### 设置传输器\n```\n目前支持三种传输器,分别为\ncurl,socket,stream\n```\n\n- 注册传输器\n```php\nuse hclient\\Client;\n$hclient = new Client();\n\n$hclient-\u003eregisterTransport('curl','hclient\\transports\\CurlTransport');\n\n```\n- 设置全局默认传输器\n```php\nuse hclient\\Client;\n$hclient = new Client();\n$hclient-\u003esetTransport('curl');\n```\n\n- 设置请求传输器\n```php\nuse hclient\\Client;\n$hclient = new Client();\n$hclient-\u003epost('http://www.baidu.com')-\u003esetTransport('curl')-\u003esend();\n```\n\n- 设置传输器相关参数\n```php\nuse hclient\\Client;\n$hclient = new Client();\n$reqeust = $hclient-\u003euri(\"md\",\"system/site/siteInit\",['id'=\u003e1]);\n\n// 设置传输协议相关参数\n$reqeust-\u003esetTransportOptions([\n    CURLOPT_RETURNTRANSFER=\u003etrue,\n    CURLOPT_POST=\u003e'POST'\n]);\n\n\n```\n\n## 错误处理\n- 验证请求错误信息\n```php\nuse hclient\\Client;\n$hclient = new Client();\n$reqeust = $hclient-\u003euri(\"md\",\"system/site/siteInit\",['id'=\u003e1]);\n$response = $reqeust-\u003esend();\n\n// 验证是否错误(验证网络,解析数据,Transport（传输层） 是否有错误)\nif ($response-\u003ehasError()) {\n    echo \"error\";\n} else {\n    echo \"succeed\";\n}\n\n// 验证是否网络错误(主要验证header http-code 状态码 是否等于20x)\nif ($response-\u003ehasNetworkError()) {\n    echo \"error\";\n} else {\n    echo \"succeed\";\n}\n\n// 获取错误信息\n$response-\u003egetError();\n\n```\n\n## 自定义格式化\n```\n目前支持三种json,xml,none(无须序列化)\n```\n\n- 自定义序列化类\n```php\nnamespace hclient\\formatters;\n\nuse hclient\\base\\Request;\n\nclass JsonFormatter implements FormatterInterface\n{\n    \n    // 实现此方法\n    public function format(Request $request):void\n    {\n        $request-\u003egetHeaders()-\u003eset('Content-Type', 'application/json; charset=UTF-8');\n        $request-\u003esetContent(json_encode($request-\u003egetData()));\n    }\n}\n```\n\n- 自定义反序列化类\n```php\nnamespace hclient\\formatters;\nuse hclient\\base\\Response;\n\n/**\n * Response json 反序列化\n *\u003cB\u003e说明：\u003c/B\u003e\n *\u003cpre\u003e\n * 略\n *\u003c/pre\u003e\n */\nclass JsonParser implements ParserInterface\n{\n    // 实现此方法\n    public function parse(Response $response):void\n    {\n        $response-\u003esetData(json_decode((string) $response-\u003egetContent(), true));\n    }\n}\n```\n\n## 自定义传输器\n```\n目前支持三种curl,socket,stream\n```\n\n- 自定义传输协议类\n```php\nnamespace  hclient\\transports;\nuse hclient\\base\\Request;\n\nclass StreamTransport extends Transport\n{\n    // 实现此方法\n    public function send(Request $request)\n    {\n        // 发送单个请求\n\n        return $request;\n    }\n    \n    // 扩展此方法\n    public function batchSend(array $requests)\n    {\n        // 批量发送多个请求\n    }\n\n}\n```\n\n## 任务列表\n- 增加协议mqtt\n- 增加异步\n- 增加连接池\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchinahehex%2Fhehep-hclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchinahehex%2Fhehep-hclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchinahehex%2Fhehep-hclient/lists"}