framework is here
http://luracast.com/products/restler/
i'm using restler as restful api for my work,
when i use backbone model save to a url, it sends and update my data as json by using 'HTTP PUT' request method, and i want to get a response from what i've putted...
if it's a HTTP POST request method i can use
// to getting content from a POST
$post_data = json_decode(file_get_contents('php://input'), true);
to get my content, but cant get anything from HTTP PUT
// can't get anything from a PUT
function putpatients($id) {
$post_data = file_get_contents('php://input');
$post_data = json_decode($post_data, true);
echo $post_data['name'];
}
the browser response blank
how do i return my data as json ???
As I commented on your question, php://input is a stream, if you read from it, it empties it.
I've never used Restler before but looking at the few examples provided in their download, it seems to indicate the submitted data is automatically passed as a parameter to your put handler..
In Restler's crud example, the Author class has a put request like this:
function put($id=NULL, $request_data=NULL) {
return $this->dp->update($id, $this->_validate($request_data));
}
so i'm guessing that restler has already read the php://input stream, and hence emptied it.
so, your put handler should maybe be more like in their example:
function putpatients($id, $request_data = NULL) {
/* do something with the $request_data */
var_dump($request_data);
}
Edit: There's actually a previous SO question from #deceze that talks about why reading twice from php://input doesn't work - for PUT requests - which explains why your code worked with a POST request. Either way, you should really use the facility provided by Restler rather than re-inventing the rest wheel.
Does the developer tool of your choice (firebug etc.) show a response?
If so it could help if you put echo json_encode($post_data['name']); instead of your echo.
try to use print_r() function for displaying the values of the variable example:
print_r($post_data);
Related
I am using Angular2 CLI for my frontend framework and using PHP for my backend.
this.http.post('assets/modify.php', '')
.subscribe(result => {
console.log("success post php file");
}
);
I want to use post method to run modify.php. However, I got error:
POST XXXXX/assets/modify.php 404 (Not Found)
I can use get method to read the PHP with the same URL, it is working fine. But how can I use Post to run the PHP.
modify.php:
<?php
//lode the file
$contents = file_get_contents('button.json');
//Decode the JSON data into a PHP array.
$contentsDecoded = json_decode($contents, true);
//Modify the counter variable.
$contentsDecoded['button1Status'] = "booked";
//Encode the array back into a JSON string.
$json = json_encode($contentsDecoded);
//Save the file.
file_put_contents('button.json', $json);
?>
The folder structure is:
app------ user--------------- user.component.ts(I am runing get or post method here)
assets----button.json
modify.php
when I use get method :
Request URL:http://localhost:4200/assets/modify.php
Request Method:GET
Status Code:304 Not Modified
Remote Address:127.0.0.1:4200
Referrer Policy:no-referrer-when-downgrade
when I use post method:
Request URL:http://localhost:4200/assets/modify.php
Request Method:POST
Status Code:404 Not Found
Remote Address:127.0.0.1:4200
Referrer Policy:no-referrer-when-downgrade
**Just for a update from people's help.
I found this and it help me figure out what happened to my scenario:
executing php files in a angular2cli app
SO I am thinking at the development stage, I need to have a web server can run PHP code.Will have a try on the build-in PHP Server.**
Try fully qualifying or at least a good relative URL. I'm going to assume assets is at your docroot, if not then adjust that root slash accordingly:
eg:
/assets/modify.php
or:
https://mywebhost/assets/modify.php
Your example PHP file doesn't seem to care about anything being sent to it, why use the POST method in the ajax, why not just use GET?
Im using
$this->input->post('name') ; to get a request posted to my url.Instead of post I need to access get as well.
Like in normal php its ,$_REQUEST is used.But what about in codeigniter standards,how is it possible?
It's outlined in the docs here: http://ellislab.com/codeigniter/user-guide/libraries/input.html
To grab data from the get you can use
$this->input->get('some_data', TRUE);
That looks for some_data in the query string, and will run is through XSS filtering to clean it from possible hack attempts
There's also a handy method to check both at the same time:
$this->input->get_post('some_data', TRUE);
"This function will search through both the post and get streams for data, looking first in post, and then in get"
Try this if you want to post request from server
if ($this->input->server('REQUEST_METHOD') == 'POST'){}
if ($this->input->server('REQUEST_METHOD') == 'GET'){}
I need to make a PATCH request to a PHP application.
How can I get the data from that PATCH request inside that application?
If I had to do it with a POST, it would just be a simple access to the global $_POST variable.
I know that this has been solved, but for anyone who was hoping for an answer like
$_PATCH["name"];
there is a way to do that:
parse_str(file_get_contents('php://input'), $_PATCH);
then you can access it like $_GET["something"] and $_POST["something"] just do
$_PATCH["something"]
hope that helped someone :)
You can get data with php://input stream wrapper:
$data = file_get_contents('php://input');
Also make sure your web server supports PATCH requests, some are configured to respond only to GET and POST.
Since none of the above has worked for me in PHP 5.6, here's a solution that actually did.
I used this parse_raw_http_request($data) function by Christof.
And here's the code:
$_PATCH = [];
parse_str(file_get_contents('php://input'), $_PATCH);
parse_raw_http_request($_PATCH);
// From now on, the $_PATCH variable keeps all request arguments as well,
// and they're accessible under approprate keys like $_PATCH['yourKey']
i'm using : PHP 7.4
function patchMethod(){
parse_str(file_get_contents('php://input'), $_PATCH);
$body=[];
if (is_array($_PATCH)) {
foreach ($_PATCH as $key => $value) {
$body[$key] = $value;
}
}
return $body;
}
You have $_REQUEST superglobal containing all data we can get regardless the HTTP method used (GET, POST, PATCH, PUT)
I'm writing my first web service, and I have a problem related to JSON data passing. I have my web service divided in two files: controller.php, which contains the service handler, and service.php, which contains the classes and methods to be served on request.
This is the acquisition fragment from controller.php:
public function atender() {
// pre-procesamos la peticiĆ³n
if (!empty($_POST)) {
if (!empty($_POST["class"]) && !empty($_POST["action"]) && !empty($_POST["function"])) {
$clase = ucwords($_POST["class"]);
$metodo = "{$_POST["action"]}{$this->obtenerMetodo($_POST["function"])}";
$id = (!empty($_POST["datos"]) ? stripslashes($_POST["datos"]) : null);
I can attend requests on both GET and POST mode (I use GET for methods not requiring authentication, like getCategories, getCategoryById, getProducts and getProductById. These are methods to get the dish categories and dishes in a sushi restaurant.)
For any of the GET requests, everything works like expected. My problem comes when I handle POST requests. I need to get all URL parameters in JSON, as this is to serve an iOS app, and JSON is the way we handle data to/from.
This is the processing fragment from service.php:
public function putUser($datos) {
if (!empty($datos)) {
$usuario = json_decode($datos);
$this->log .= implode("/", $usuario) . "\r\n";
In this case, $datos is the JSON-encoded data from the request. It's received as $id in controller.php (the code above). As it's my first web service, it's very probable I'm doing something really bad here, but I'm a bit blinded.
I've tried different variations of the service handling code. Using json_decode($datos, true) doesn't work either. I get
'Unexpected token <' as a response and, in raw form (using the advanced REST client from Google Chrome) it says: ''Warning: implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in /home/refine/public_html/sushigo/palma/service.php on line 344'.
I know SOAP is, usually, a better alternative to writing custom code like this but, for now, I need to stick with this code and implement a better alternative for my next project. Could you tell me what am I doing wrong?
The error message says, basically, that it is NOT a valid JSON - and such error messages are usually right.
Your error is somewhere in the sending / receiving code. Probably you send the JSON in one form and try to access it in some other way. Since I have no way of looking at the requests sent from the phone, I would guess that:
you send the data as application/json and try to receive it as an url encoded form. If you don't understand the difference, here's your problem.
you use stripslashes on the JSON data, which is wrong. UNLESS you have magic_quotes turned on, which would also be wrong (that is: both magic_quotes and stripslashes have to go).
I managed to make a bunch of webservices with symfony using GET parameters, but i need to use POST parameters for a signup webservice.
But when I try to get those POST parameters, for example $request->getParameter('test');, they just return a null value.
How comes ?
EDIT
Here's a straightforward exemple
testSuccess.json.php
{
"test": <?php echo json_encode($test); ?>
}
action.class.php
public function executeTest(sfWebRequest $request) {
$this->test = $request->getParameter('test');
}
That would indicate that there isn't a POST parameter called 'email' sent with your request. Apart from that there's not much anyone else can say about your problem without additional information.
I have run into the same problem. It seems that when one does not use
Content-Type: application/x-www-form-urlencoded
these parameters are not available in Symfony. I cannot find anything in the http documentation that dictates this content type for these methods. I have created a symfony_ticket for this bug.
So, to fix your issue add this Content-Type header to your request.
There is a way around the issue with the POST parameters being lost with JSON object posts. Instead of using the various parameter methods you need to use the:
$request->getContent()
Which shows the JSON as a string within the body of the post. Having the content set as application/json, Symfony effectively ignores the content so doesn't pickup the parameters in the body.