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

房东的猫 提交于 2019-11-26 14:35:13

问题


-- Question --

I am just starting out with the REST API and am getting pretty confused.

This is what my PHP cRUL client-side looks like for a PUT.

case 'PUT':
    curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
    break;

Now when I look at the server my $_SERVER['REQUEST_METHOD'] shows PUT, but my question is how do I get the $data I sent with CURLOPT_POSTFIELDS.

All I need to do is get the $data sent with a PUT request into the next line. Like

$value = $data['curl_data'];

I have seen so much clutter on this topic that it is giving me a headache. It seems so easy on the php client side, but no one has answers that are working for the php server side.

Thanks for any help!

-- Answer (after help and homework) --

I am new so I can't answer my own question until after 8 hours... odd :)

Okay, after working with the great people here I have to say we ran into the answer. I am kicking myself for it being so easy, at the same time it was confusing.

curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($data));

The first change (above) I had to add http_build_query() around $data. This took my data from an array to a url friendly string.

Next up I had to add.

parse_str(file_get_contents('php://input'), $put);

Now I can do things like $put['data'].

The answer PaulPRO gave above does work to get the data the same way file_get_contents() did with less lines. We got stuck trying to figure out how to parse the data which was where my lack of http_build_query() I had seen on another site kicked into play.

So This is how it all works.

  1. Data is put into a normal array.
  2. http_build_query() converts it into a nice almost GET like string.
  3. file_get_contents() transports it from the client to the server.
  4. parse_str() then turns it back into an array.

I am seeing a lot of messages about using PUT to send files. I can see how this would work, but from what I read in this entire REST process was that PUT is to update data as post is to create data. Maybe I am mistaken. Am I missing something?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/6805570/how-do-i-access-php-rest-api-put-data-on-the-server-side

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