⊗ppSpAiPPJ 47 of 83 menu

API With POST Parameters in JSON Format in PHP

Sometimes parameters are arrays. In this case, such parameters should be packed into JSON. Let's look at an example. Suppose we have the following API, expecting an array in JSON format and returning the sum of the elements of this array:

<?php echo array_sum(json_decode($_POST['json'], true)); ?>

Let's make a request to this API:

<?php $url = 'http://api.loc/index.php'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); $arr = [1, 2, 3, 4, 5]; $json = json_encode($arr); $data = ['json' => $json]; curl_setopt($curl, CURLOPT_POSTFIELDS, $data); $res = curl_exec($curl); var_dump($res); ?>

Create an API that will accept an array of dates as a parameter, and return an array of historical events that happened on the passed dates.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline