Download the PHP package dd/evolutioncms-snippets-ddmakehttprequest without Composer
On this page you can find all versions of the php package dd/evolutioncms-snippets-ddmakehttprequest. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dd/evolutioncms-snippets-ddmakehttprequest
More information about dd/evolutioncms-snippets-ddmakehttprequest
Files in dd/evolutioncms-snippets-ddmakehttprequest
Package evolutioncms-snippets-ddmakehttprequest
Short Description Makes HTTP request to a given URL.
License
Homepage https://code.divandesign.ru/modx/ddmakehttprequest
Informations about the package evolutioncms-snippets-ddmakehttprequest
(MODX)EvolutionCMS.snippets.ddMakeHttpRequest
Makes HTTP request to a given URL.
- Supports redirects even if native CURL redirects is unavailable.
- Automatic response validation with configurable success/failure checking.
- Flexible data conversion and output formatting.
- All HTTP methods (GET, POST, PUT, PATCH, DELETE).
- Cookies, proxy, custom headers.
Requires
- PHP >= 7.4
- (MODX)EvolutionCMS.libraries.ddTools >= 0.67
Installation
Using (MODX)EvolutionCMS.libraries.ddInstaller
Just run the following PHP code in your sources or Console:
- If
ddMakeHttpRequestis not exist on your site,ddInstallerwill just install it. - If
ddMakeHttpRequestis already exist on your site,ddInstallerwill check it version and update it if needed.
Manually
1. Elements → Snippets: Create a new snippet with the following data
- Snippet name:
ddMakeHttpRequest. - Description:
<b>2.4.1</b> Makes HTTP request to a given URL.. - Category:
Core. - Parse DocBlock:
no. - Snippet code (php): Insert content of the
ddMakeHttpRequest_snippetfile from the archive.
2. Elements → Manage Files
- Create a new folder
assets/snippets/ddMakeHttpRequest/. - Extract the archive to the folder (except
ddMakeHttpRequest_snippet.php).
Parameters description
General parameters
isDebug- Description: Log all requests to event log (including successful ones), not only errors. Useful for debugging.
- Valid values:
01
- Default value:
0
Request parameters
-
requester- Description: Request parameters.
- Valid values:
stringJsonObject— as JSON objectstringHjsonObject— as HJSONstringQueryFormatted— as Query string- It can also be set as a native PHP object or array (e. g. for calls through
\DDTools\Snippet::runSnippet):arrayAssociativeobject
- Default value: —
-
requester->url- Description: The URL to fetch.
- Valid values:
string - Required
-
requester->method- Description: Request type.
- Valid values:
'get''post''put''patch''delete'
- Default value:
'get'
-
requester->data- Description: The full data to send in request body. Can be used with POST, PUT, PATCH, DELETE methods.
- Valid values:
stringJsonObject— as JSON objectstringHjsonObject— as HJSONstringQueryFormatted— as Query stringstring- It can also be set as a native PHP object or array (e. g. for calls through
\DDTools\Snippet::runSnippet):arrayAssociativeobject
- Default value: —
-
requester->isRawDataEnabled- Description: Send raw
data. E. g. if you need JSON in request payload. - Valid values:
01
- Default value:
0
- Description: Send raw
-
requester->headers- Description: An array of HTTP header fields to set.
- Valid values:
stringJsonArray— as JSONstringHjsonArray— as HJSONstringQueryFormatted— as Query string- It can also be set as a native PHP array (e. g. for calls through
\DDTools\Snippet::runSnippet):array
- Default value: —
-
requester->userAgent- Description: The contents of the
User-Agent:header to be used in a HTTP request. - Valid values:
string - Default value: —
- Description: The contents of the
-
requester->timeout- Description: The maximum number of seconds for execute request.
- Valid values:
integer - Default value:
60
-
requester->proxy- Description: Proxy server in format
[+protocol+]://[+user+]:[+password+]@[+ip+]:[+port+]. E. g.http://user:[email protected]:5555orsocks5://user:[email protected]:5555. - Valid values:
string - Default value: —
- Description: Proxy server in format
requester->isCookieUsed- Description: Enable cookies. The
assets/cache/ddMakeHttpRequest_cookie.txtfile is used. - Valid values:
01
- Default value:
0
- Description: Enable cookies. The
Data processing parameters
-
dataProcessor- Description: Response data processing and validation parameters for automatic success/failure checking.
- Valid values:
stringJsonObject— as JSON objectstringHjsonObject— as HJSONstringQueryFormatted— as Query string- It can also be set as a native PHP object or array (e. g. for calls through
\DDTools\Snippet::runSnippet):arrayAssociativeobject
- Default value: — (see below)
-
dataProcessor->checkValue- Description: Value considered as success/failure (depends on
dataProcessor->isCheckForSuccess). - Valid values:
mixed(string, number, boolean, etc.) - Default value:
''(empty string)
- Description: Value considered as success/failure (depends on
-
dataProcessor->isCheckForSuccess- Description: Whether to check for success or failure.
- Valid values:
true— check for success (e. g. response:{"success": true})false— check for failure (e. g. response:{"error": true})
- Default value:
false
-
dataProcessor->checkPropName- Description: Name of the response property to check for success/failure status.
- Use only if the response is an object.
- You can also use
.to get nested properties. Examples:error,ok,success,status— get first-level propertysms.status— get second-level property
- Valid values:
null— check whole response data (usually if response is not an object)string— property name for checking (if response is object)
- Default value:
null
- Description: Name of the response property to check for success/failure status.
-
dataProcessor->messagePropName- Description: Name of the response property that contains message text (success or error message).
- Use only if the response is an object.
- You can also use
.to get nested properties. Examples:description,title,message— get first-level propertyerror.message— get second-level property
- Valid values:
null— do not extract messagestring— property name with message
- Default value:
null
- Description: Name of the response property that contains message text (success or error message).
dataProcessor->convertTo- Description: Convert response data to specified type.
- Important: This converts only the
datapart of the response. If you need to convert the whole snippet result (which can bedata,meta, ormetaData), useoutputter->convertToinstead. - If parameter is set, data is always returned in specified format, regardless of success/failure status and even if data is invalid (you don't need to do extra checks yourself).
- Values are case insensitive (the following values are equal:
'stringjsonauto','stringJsonAuto','STRINGJSONAUTO', etc).
- Important: This converts only the
- Valid values:
''(empty value) — do not convert (default value)- The snippet can convert data to primitive types:
'string''integer'/'int''float''boolean'/'bool'
- The snippet can convert data to string:
'stringJsonAuto'—stringJsonObjectorstringJsonArraydepends on data type'stringJsonObject''stringJsonArray''stringQueryFormatted'— Query string
- The snippet can also convert data to native PHP object or array (it is convenient to call through
\DDTools\Snippet):'objectAuto'—stdClassorarraydepends on data type'objectStdClass'—stdClass'objectArray'—array
- Default value: — (without conversion)
- Description: Convert response data to specified type.
Output parameters
-
outputter- Description: Output parameters.
- Valid values:
stringJsonObject— as JSON objectstringHjsonObject— as HJSONstringQueryFormatted— as Query string- It can also be set as a native PHP object or array (e. g. for calls through
\DDTools\Snippet::runSnippet):arrayAssociativeobject
- Default value:
-
outputter->type- Description: What to return as snippet result.
- Values are case insensitive (the following values are equal:
'metaData','metadata','METADATA', etc).
- Values are case insensitive (the following values are equal:
- Valid values:
'data'— response body only'meta'— only metadata, the following properties are available:'isSuccess'— Whether the request was successful (CURL, HTTP code, and data validation)'isCurlSuccess'— Whether CURL execution was successful'isHttpCodeSuccess'— Whether HTTP response code is not an error (< 400 or >= 600)'isDataValid'— Whether response data is valid'effectiveUrl'— Effective URL'curlErrorCode'— CURL error code'message'— Message text. Contains CURL error message if CURL failed, or message from response data ifdataProcessor->messagePropNameis set'code'— HTTP code
'metaData'— both response body and metadata as JSON object withdataandmetaproperties
- Default value:
'data'
- Description: What to return as snippet result.
outputter->convertTo- Description: Convert the whole snippet result to specified format.
- Important: This converts the complete snippet result (which can be
data,meta, ormetaDatadepending onoutputter->type). If you need to convert only the responsedata, usedataProcessor->convertToinstead. - Values are case insensitive (the following values are equal:
'stringjsonauto','stringJsonAuto','STRINGJSONAUTO', etc).
- Important: This converts the complete snippet result (which can be
- Valid values:
''(empty value) — return as is, without conversion (default value)- The snippet can return object as string:
'stringJsonAuto'—stringJsonObjectorstringJsonArraydepends on result object'stringJsonObject''stringJsonArray''stringQueryFormatted'— Query string
- The snippet can also return object as a native PHP object or array (it is convenient to call through
\DDTools\Snippet).'objectAuto'—stdClassorarraydepends on result object'objectStdClass'—stdClass'objectArray'—array
- Default value: — (without conversion)
- Description: Convert the whole snippet result to specified format.
Examples
Simple GET request
Simple POST request
Set data as HJSON:
Or Query string:
Run the snippet through \DDTools\Snippet::runSnippet without DB and eval
Get only meta
Get both data and meta
Convert result to JSON
Convert result to array
Response data validation with dataProcessor
Validate response with simple value (e. g. string or number)
API returns simple value and if it equals to OK — it's success, otherwise — failure.
Validate response as object and check for success status
API response is an object and contains success property with value true (e. g. {"success": true}) — it's success, otherwise — failure.
Validate response as object, check for failure status and extract error message
API response is an object and contains status property with value fail (e. g. {"status": "fail"}) — it's failure, otherwise — success.
Convert data and output simultaneously with dataProcessor->convertTo and outputter->convertTo
API returns JSON string '{"userId": "123", "userName": "John"}'. We want to:
- Convert response data from JSON string to PHP object (using
dataProcessor->convertTo) - Return both data and meta (using
outputter->type = 'metaData') - Convert the whole result to JSON for frontend (using
outputter->convertTo)