How do I access PHP REST API PUT data on the server side?

為{幸葍}努か 提交于 2019-11-27 09:13:44

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);
Srinivasu

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..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!