Viewed   90 times

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

 Answers

2

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);
Saturday, August 13, 2022
5

You need to handle authentication and authorization for your APIs.

These are very basic steps to understand the solution:

  • Client calls login API using user credentials(username, password).
  • Server authenticates user credentials and generates a token.
  • Server stores this token in database against authenticated user id and responds to client.
  • Server already has authorization role rights to access different APIs associated with this authenticated user in database.
  • Client calls resource APIs using token provided by login API.
  • Server verify token in database and fetch user and user role rights against this token for authorization.
  • Resource APIs authorize and provides required data or perform actions according to authenticated user role rights.

There are multiple ways to achieve this in standardize way:

  • 3 Common Methods of API Authentication Explained
  • JSON Web Token
  • oAuth2.0
Wednesday, August 3, 2022
 
1

I had the same error message.

For me it occured when the javascript code receieved the response from my CREATE_PAYMENT_URL ajax request.

The response I was sending back from the server was not valid JSON so when it came time to execute onAuthorize data.paymentID and data.payerID were not available.

I suggests using console.log(data) just above the return statement in onAuthorize to make sure the data json object has a paymentId and payerId.

If not then it may be that your previoius CREATE_PAYMENT_URL is not returning the payment id.

The responses should be json. For example a valid create payment response:

{"id":"PAY-5T1130394T551090NLGRLACY"}

Hope that helps.

Wednesday, December 7, 2022
 
mia
 
mia
1

Depending upon your preferred method of modifying PHP variables (Apache's config, .htaccess), change the session.cookie_domain value to be a consistent value.

I have multiple sub-domains, and each VirtualHost section in the Apache config file contains the following line:

php_value session.cookie_domain mydomain.com

The syntax should be similar if you make the changes in a .htaccess file.

Updated for bobert5064's comment:

For multiple domains (ie domain1.com, domain2.org), I think it is only necessary to choose a common domain name (ie domain1.com). I have never tried this, so I cannot verify that it works, but the logic seems accurate.

There is also a method to set the variables direction in PHP described at http://us.php.net/manual/en/function.session-set-cookie-params.php. The documentation makes no reference to the ability or inability to set cookies on a different domain.

Thursday, September 29, 2022
 
josiah
 
3

First of all you should read up on and learn how to use Jersey. This is the best implementation of JAX-RS API (REST for Java).

You will also need a good HTTP server and Java Servlet container. For that I'd advise you to use Jetty. Here are a few good tutorials:

  1. Crunchify: Build RESTful Service in Java using JAX-RS and Jersey (Celsius to Fahrenheit & Fahrenheit to Celsius)
  2. Vogella: REST with Java (JAX-RS) using Jersey - Tutorial

You asked for a "simple server", however if you're looking for high performance then read this tutorial: Javarants: Using JAX-RS with Protocol Buffers for high-performance REST APIs, and use the Grizzly container instead of Jetty:

Thursday, October 27, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :