call matlab function from php passing JSON object and return some values
3 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Kunal Kandhari
2021-6-22
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:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 JSON Format 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!