call matlab function from php passing JSON object and return some values

1 view (last 30 days)
I want to call matlab function from php passing JSON object and return some numric values from that matlab function to be used in php.

Answers (1)

Kunal Kandhari
Kunal Kandhari on 22 Jun 2021
This can be done by establishing the Socket communication between PHP and Matlab
Example code for it:
Code for PHP:
<?php
$host = "127.0.0.1";
$port = 5002;
$data = array(
'username' => 'codexworld',
'password' => '123456'
);
$payload = json_encode($data);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
socket_write($socket, utf8_encode($payload), strlen($payload)) or die("Could not send data to server\n");
$response = socket_read($socket, 8);
echo $response;
socket_close($socket);
?>
Code for MATLAB:
server = tcpserver("127.0.0.1",5002,"ConnectionChangedFcn",@connectionFcn)
function connectionFcn(src, ~)
if src.Connected
disp("Client connected")
rawData = read(src,src.NumBytesAvailable,'uint8');
jsonString = native2unicode(rawData, 'ISO-8859-1');
disp(jsonString);
structValues=jsondecode(jsonString);
disp(structValues);
result=56;
write(src,result,"int8");
end
end
Output PHP:
Output MATLAB:
You can refer following docs for more details:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!