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

9 views (last 30 days)
Mohamad Alsioufi
Mohamad Alsioufi on 21 Oct 2016
Answered: Kunal Kandhari on 22 Jun 2021
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:
$host = "";
$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;
Code for MATLAB:
server = tcpserver("",5002,"ConnectionChangedFcn",@connectionFcn)
function connectionFcn(src, ~)
if src.Connected
disp("Client connected")
rawData = read(src,src.NumBytesAvailable,'uint8');
jsonString = native2unicode(rawData, 'ISO-8859-1');
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!