I'm new to making API. I want to know, how to call a post API with multiple parameters? I know get API can be called with multiple parameters but is there any way to call a POST API like that? here is my code:
api.php :
if ($action == 'user_assign_machine') {
$id = $PARAMS['id'];
$user_Id = $PARAMS['user_Id'];
$res = HR::userAssignMachine($user_Id,$id);
echo json_encode($res);
code-api.php :
public static function userAssignMachine($userid,$machineid) {
$message_to_admin = "Hi Admin! User $user_Id wants to assign machine $machineid";
return $message_to_admin;
}
Now I want to call this API through a link, i.e. passing all the parameters in the API URL link. plz help me out. thanks
for post requests from PHP, you could use curl
public static function userAssignMachine($userid,$machineid,$url) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'userid' => $userid,
'machineid' => $machineid
)
));
$resp = curl_exec($curl);
curl_close($curl);
}
You can use curl send the multiple data using post
This is the best way for creating apis
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "http://www.example.com/yourscript.php",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'field1' => 'some date',
'field2' => 'some other data',
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
// result sent by the remote server is in $result