jsobeslav/webproxy

WebProxy 是一个 PHP 库,它提供了一个统一的接口,用于与标准第三方网络服务进行通信,无论是 API 还是普通网站。

0.1.6 2019-08-05 12:55 UTC

This package is auto-updated.

Last update: 2024-09-06 00:13:16 UTC


README

  • WebProxy 是一个 PHP 库,它提供了一个统一的接口,用于与标准第三方网络服务进行通信,无论是 API 还是普通网站。
  • 它提供了解析 REST API 的 JSON 响应,从网站抓取和爬取 HTML 代码,或调用 SOAP 服务的方法。
  • 为了澄清某些潜在的误解,请参阅以下术语章节。

包含的库/依赖

用法

示例

  • 有关实现示例,请参阅 demo 文件夹。

一般指南

  • 创建扩展合适子类的 Service 类
    • 选项有
      • RestApi(用于 REST API)
      • Website(用于通过网站爬取)
      • SoapService(用于 SOAP API)
    • 填写缺失的接口方法
      • getUri():返回服务所在的基础 URI。
        • 对于 HttpService,它是基础 URI。例如:return 'https://jsonplaceholder.typicode.com';
        • 对于 SoapService,它是 WSDL URI。例如:return 'https://soapexample.com?wsdl';
  • 创建扩展选定 Service 类型适当子类的 Endpoint 类
    • 选项有
      • RestResource(用于 RestApi
      • Webpage(用于通过 Website 爬取)
      • SoapEndpoint(用于 SoapService
    • 填写缺失的接口方法和可选类变量
      • getServiceClass():返回适当服务的类。例如:return JsonPlaceholderRestApi::class;
      • getRequestName():返回端点执行的请求规范。
        • 对于 HttpService,它是基础 Service URI 后附加的 URI。例如:return '/posts';
        • 对于 SoapService,它是方法名。例如:return 'getPosts';
      • $supportedMethods:HttpServices 的可选白名单。例如:return [Method::POST];
    • 编写方法以过滤结果
      • 对于 RestResource,利用解析的数据对象:return $this->getResponseData()['id'];
      • 对于 Websites,利用 Symfony DOM Crawler:return $this->getCrawler()->filter('.address h3')->text();
      • 对于 SoapEndpoints,利用解析的数据:return $this->getResponseData()['result'];
  • 初始化 WebProxy 对象(new WebProxy())和所需的端点(new ExampleResource()
  • 通过其方法之一将自定义端点的新实例传递给 WebProxy
    • getpostputdelete 作为 HTTP 请求的缩写
    • httpRequest 用于自定义 HTTP 请求
    • call 作为 SOAP 请求的缩写
    • soapRequest 用于自定义 SOAP 请求
  • 对于特定目的,您可能需要使用具有自定义 Request 对象的 httpRequestsoapRequest 方法
    • 例如,默认 POST 正文类型是表单数据,或 Multipart(如果随它发送文件)。要发送 JSON,请使用 Request::create(Method::POST)->withJsonBody(['array','content'])
    • Request 的实例作为第二个参数传递到提到的方法中
  • 使用返回的修改后的端点实例返回解析的数据

使用的术语

  • Client:负责在位于 ServiceEndpoint 上执行请求并获取响应体的类
    • HttpClient:包含一个Guzzle实例以执行所有请求
    • SoapClient:将请求传递给每个独立的SoapService的自己的SoapClient实例(原生)
  • Service:位于单个域上的端点集合。
    • HttpService:可通过HTTP请求访问的服务
      • RestApi:包含多个RestResource的REST API
      • Website:包含多个Webpage的网站
    • SoapService:可通过SOAP调用访问的服务
  • EndpointService的一个特定功能
    • HttpEndpointHttpService上的特定URI
      • RestResource:RESTful的HttpEndpoint
      • Webpage:返回HTML的HttpEndpoint
    • SoapEndpointSoapService的方法