How do I access PHP REST API PUT data on the server side?
From the PHP Manual:
PUT data comes from stdin:
$putdatafp = fopen("php://input", "r");
Example usage:
$putfp = fopen('php://input', 'r');$putdata = '';while($data = fread($putfp, 1024)) $putdata .= $data;fclose($putfp);
I've the same scenario where in, have to send data to the PHP Server through ReST API using the PUT method. I struggled almost couple of hours to find the solution, but finally found the way :
In CUrl :
$postData = http_build_query($data);curl_setopt($ch, CURLOPT_POSTFIELDS,$postData);
We've to parse the data to a variable let say: $putData, Here, is the Parse String procedure :
parse_str(file_get_contents("php://input"),$putData);
Then print the $putData
, will get the same array that you're posting in the curl..
If you want to get form data with key value like $_POST.
function PUT(string $name):string{ $lines = file('php://input'); $keyLinePrefix = 'Content-Disposition: form-data; name="'; $PUT = []; $findLineNum = null; foreach($lines as $num => $line){ if(strpos($line, $keyLinePrefix) !== false){ if($findLineNum){ break; } if($name !== substr($line, 38, -3)){ continue; } $findLineNum = $num; } else if($findLineNum){ $PUT[] = $line; } } array_shift($PUT); array_pop($PUT); return mb_substr(implode('', $PUT), 0, -2, 'UTF-8');}
For Example rest-api.php
$title = PUT('title');