call matlab function from php passing JSON object and return some values
Mostra commenti meno recenti
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.
Risposte (1)
Kunal Kandhari
il 22 Giu 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:
Categorie
Scopri di più su JSON Format in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!