jsobeslav / webproxy
WebProxy is PHP library that offers consistent interface for communicating with standard third-party web services, be it API or an ordinary website
Installs: 133
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 2
Open Issues: 1
pkg:composer/jsobeslav/webproxy
Requires
- php: >=7.1
- consistence/consistence: ^1.1
- guzzlehttp/guzzle: ^6.3
- symfony/css-selector: ^4.1
- symfony/dom-crawler: ^4.1
Requires (Dev)
- codeception/codeception: ^2.5
- phpmd/phpmd: ^2.6
- squizlabs/php_codesniffer: ^3.4
README
- WebProxy is PHP library that offers consistent interface for communicating with standard third-party web services, be it API or an ordinary website.
- It offers parsing JSON response from REST API, scrapping and crawling HTML code from website, or calling methods on SOAP services.
- To clarify certain potential misconceptions, please see terminology chapter below.
Included libraries / dependencies
- For HTTP:
guzzlehttp/guzzle| http://docs.guzzlephp.org/en/stable/ - For HTML crawling:
symfony/dom-crawler| https://symfony.com/doc/current/components/dom_crawler.html - For SOAP: native
SoapClient| https://secure.php.net/manual/en/class.soapclient.php
Usage
Examples
- For implemented examples, see
demofolder.
General guide
- Create Service class that extends suitable child
- Options are:
RestApi(for REST APIs)Website(for crawling trough Websites)SoapService(for SOAP APIs)
- Fill in the missing interface methods
getUri(): return base URI where service is located.- For
HttpServiceit's base URI. E.q.return 'https://jsonplaceholder.typicode.com'; - For
SoapServiceit's WSDL URI. E.q.return 'https://soapexample.com?wsdl';
- For
- Options are:
- Create Endpoint classes that extends children appropriate to selected Service type
- Options are:
RestResource(forRestApi)Webpage(for crawling troughWebsite)SoapEndpoint(forSoapService)
- Fill in the missing interface methods, and optional class variables
getServiceClass(): Return class of appropriate service. E.q.return JsonPlaceholderRestApi::class;getRequestName(): Return specification of request that the endpoint performs.- For
HttpService, it's URI appendend after base Service URI. E.q.return '/posts'; - For
SoapService, it's method name. E.q.return 'getPosts';
- For
$supportedMethods: Optional whitelist for HttpServices. E.q.return [Method::POST];
- Write methods to filter the results
- For
RestResource, utilize parsed data object:return $this->getResponseData()['id']; - For
Websites, utilize Symfony DOM Crawler:return $this->getCrawler()->filter('.address h3')->text(); - For
SoapEndpoints, utilize parsed data:return $this->getResponseData()['result'];
- For
- Options are:
- Initialize
WebProxyobject (new WebProxy()) and desired endpoints (new ExampleResource()) - Pass new instance of the custom endpoint to
WebProxytrough one of its methodsget,post,put,deleteas shorthands for HTTP RequestshttpRequestfor customized HTTP Requestscallas shorthand for SOAP requestsoapRequestfor customized SOAP Requests
- for specific purposes, you might want to use
httpRequestandsoapRequestmethods with customRequestobject- For example, default POST body type is Form-data, or Multipart (if files are sent with it). To send JSON, use
Request::create(Method::POST)->withJsonBody(['array','content]) - pass instance of the
Requestas second parameter to mentioned methods
- For example, default POST body type is Form-data, or Multipart (if files are sent with it). To send JSON, use
- Use returned modified instance of endpoint to return parsed data
Terminology used
Client: A class that is responsible for performing request onEndpointlocated onService, and fetching the response bodyHttpClient: Contains single Guzzle instance to perform all requestsSoapClient: Passes requests to every independentSoapService's own instance of nativeSoapClient
Service: A collection of endpoints located on single domain.HttpService: Service acessible trough HTTP requestsRestApi: REST API containing multipleRestResourcesWebsite: Site containg multipleWebpages
SoapService: Service accessible trough SOAP calls
Endpoint: One specific function ofServiceHttpEndpoint: Specific URI onHttpServiceRestResource: RESTfulHttpEndpointWebpage:HttpEndpointthat returns HTML
SoapEndpoint: A method ofSoapService