This translation is older than the original page and might be outdated. See what has changed.
Translations of this page:
 

sendJSON(), sendPOST() and sendXML()

The comand sendJSON() sends a HTTP-request to the URL and returns the answer of the server.

mixed sendJSON(string URL, array data)
mixed sendPOST(string URL, array data)
mixed sendXML(string URL, string XML)

  • URL
    The URL to which the HTTP request should be sent.
  • Daten
    The data which should be transmitted in JSON or POST format.
  • XML
    An XML string which is to be transmitted.

Warning: The request is performed synchronously, which means that it delays the loading time of the questionnaire page by the time until the requested server sends a response. The request is aborted if the loading time takes more than 5 seconds.

Note: The amount of requests is limited (s. Limitations).

Return Value sendJSON()

If the request could not be processed due to an error, false is returned.

If the remote peer sends a response in JSON format, it is returned as an array.

Otherwise the answer from the server is returned as a string.

Return Value sendPOST()

If the request could not be processed due to an error, false is returned.

Otherwise the answer from the server is returned as a string.

Return Value sendXML()

If the request could not be processed due to an error, false is returned.

When the server submits valid XML data, it is decoded and returned as an array, where each XML tag is represented in an array with the following keys:

  • string 'name' – Attribute name
  • array 'attr' – Attributs in the XML-tag
  • string 'content' – Possible text content of the XML-tag
  • array 'children' – Possible kind-elements of the XML-tag

Otherwise the answer from the server is returned as a string.

Limitations

  • In a survey project, a maximum of 1000 requests can be sent within 24 hours.
  • Within a survey, a maximum of 20 requests can be sent, within a page a maximum of 2 requests.
  • It is not possible to send requests to the survey surver itself.

Example

$data = sendJSON('https://example.com', [
  'q' => 'reflect',
  'd' => 'SoSci Survey'
]);
debug($data);

Test

If the remote station does not respond as you would expect, you should first check your request.

SoSci Survey provides a small script at https://www.soscisurvey.de/tools/post-monkey.php. There you will receive a URL to which you can send a request. Then you can check the content of the request.

If you believe the request is correct, view the response. The easiest way to send the request to the API once is through your browser's developer tools (Browser Developer Tools). Alternatively, you can take a closer look at the return value of the function using debug() (see above).

en/create/functions/sendjson.txt · Last modified: 01.06.2022 19:50 by admin
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Driven by DokuWiki