'How can I retrieve multiple id from api in one request?
This's API request.
curl --location --request POST 'http://1455.api.123/xx' \
--header 'ApiPass: *******\
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": "null",
"method": "person",
"params": {
"id": 15 // user ID in DB
}
}'
I'm use this code for call request.
public function call_data($id){
$apiKey = '***********';
$apiUrl = 'http://1455.api.123/xx';
$message = json_encode(
array('jsonrpc' => '2.0', 'id' => 'null', 'method' => 'person', 'params' => array('id'=>$id));
);
//$sign = hash_hmac('sha512', $message, $apiSecret);
$requestHeaders = [
'ApiPass:' . $apiKey,
'Content-type: application/json'
];
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
$response = curl_exec($ch);
curl_close($ch);
$arrayData = json_decode($response,true);
$arrayDataUsers = $arrayData;
return $arrayDataUsers;
}
How can I retrieve multiple id from api in once request where i don't want to use loop call_data()?
Note. One request for this code has load time about 0.5sec.
Solution 1:[1]
It depends on what capabilities the API provides you. If there is any API that allows you to access multiple IDs at once then you can use it, otherwise unfortunately you'll have to resort to loop only.
If you want to increase the speed of retrieving the results, you might try looking into parallel retrieval of the data but it totally depends on your use case on how you are using the user data ( or if the API limits your parallel requests or not. )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MANORIT CHAWDHRY |
