how to run PHP in a Angualr2 CLI - php

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?

Related

Passing POST parameters to PHP does not work

I am sending parameters to a PHP file as a POST request, direct from my browser like :
example.com/
with example.php?CID=1
But not getting the values in the PHP file.
I am sending parameters to a PHP file as a POST request, direct from my browser like
in the php file i am trying to read :
$cid = $_POST["CID"];
$cname = $_POST["CNAME"];
But the local variables are null after executing this.
I also tried doing this :
echo var_dump($_POST);
and it returns :
array(0)
i am hosting this on a paid hosting server, and the configuration allows upto 1000 parameters.
The way you have set this up you are using the get method
$cid = $_GET["CID"];
$cname = $_GET["CNAME"];
If you are wanting to use post then please read the PHP manual
https://www.php.net/manual/en/reserved.variables.post.php
first of all try this PHP "php://input" vs $_POST
php://input
it will solve your issues and let you process developement as workaround - it is very usefull when communicating with pure json not as jason passed into Post field.
next make a check configuration of your server - i had the same problem with nginx and solved it somehow but do not remember how ;)

Python to PHP on server send image

I have a raspberry pi running a lamp stack, an arduino and a camera hooked up. The end goal is that when my arduino takes a photo, it then writes an image to a php address which is then emailed.
Right now, I'm trying to get the image to get placed in the right place.
Here's my php snippet:
<?php
print_r($_FILES);
move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/images/mypic.jpg");
?>
My python code is doing:
import requests
r = requests.get('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png')
r2 = requests.post('http://192.168.1.100/accept_image.php', data = r.content)
I realize the image is going to get overwritten. That's not a problem. I can always add a timestamp later etc etc.
However, this gives me an error code. I'm a beginner at php and use python mainly for scientific computing so not sure if I'm passing the picture correctly. I know that the ip is correct as I can connect to it and it's all in network.
I have looked at this Python script send image to PHP but am still getting stuck.
EDIT:
Upon further debugging:
print_r($_POST);
returns an empty array. Not sure why?
To have a file accessible to PHP in $_FILES, you must use HTML-form style encoding of files (multipart/form-data). This is different from a standard POST request including the content in the request body. This method is explained in http://php.net/manual/en/features.file-upload.post-method.php - the key here is:
PHP is capable of receiving file uploads from any RFC-1867 compliant browser.
What's you're trying to do is not sending it the RFC-1867 way, but a plain-old POST request.
So you have two options:
Send the data from Python using multipart/form-data encoding. It shouldn't be too hard but requires some work on the Python side.
Just grab the data on the PHP side not from $_FILES but by directly reading it from the POST body, like so:
.
$data = file_get_contents('php://input');
file_put_contents("/var/www/images/mypic.jpg", $data);
It's more of a memory hog on the PHP side, and means you need to do some validation that you actually got the data, but is quite simpler.
To clarify, in PHP $_POST is only populated when the Content-type request header is multipart/form-data or application/x-www-form-urlencoded (and of course the data is encoded in the proper way).
If you get a POST request with anything else in the body, you can read it directly by reading from the php://input stream, and you're responsible for handling / decoding / validating it.

How to send data from one site and receive it on another in php

I have data in string format in a single variable in a php file on a wordpress site.
I want to fetch that variable's value through a php file on different server.
I want a way which will send that variable to my receiving php file that I have created on different server and print that values here.
In short, e.g. let there is data in mydomain1.com/send.php
which need to be stored or displayed in mydomain2.com/receive.php
But, without using form.There is no html form in sending file and also I don't want it since no redirection should be done.Just on a function execution in sending file data need to be transferred and displayed only on receiving end.
(I tried to find out solution for this using cURL.But, everywhere I found code to send data but what about receiving data, how can I capture that sent data and display at receiving end.)
If there is another solution except cURL or form submission I would appreciate.
Please help soon.
there are a lot of ways to do this, one way would be a SOAP client/server solution..:
you have basically 2 php files, one file on server1 is let say the client.php and on the other server there is the file named server.php which will receive all the data sent from client.php on server 1... here is a simple source, you need to change the URLs in the script to your server/client URLs so it works..:
client.php
<?php
//This is the SOAP Client which will call a method on Server 2 with passing some data:
//uri is the location where client.php is located, and "location" is the exact location to the client, including the name "client.php"
$client=new SoapClient(NULL,array("uri"=>"http://localhost/test","location"=>"http://localhost/test/test.php"));
$message=$client->hello("Hello World");
echo($message);
?>
server.php
<?php
//This is the SOAP Server
class server2{
public function hello($data){
return "I received following data: " . $data;
}
}
//the URI here is the location where the server.php is located.
$settings = array("uri"=>"http://localhost/test/");
$s=new SoapServer(null,$settings);
$s->setClass("server2");
$s->handle();
?>
Here's a tutorial: http://davidwalsh.name/execute-http-post-php-curl
Basically you can send the urlencoded values using CURLOPT_POSTFIELDS

Can't get 'http put' content in PHP

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

how to read an http get request from a php server

I have a client in php who make an http get request to a server. that's the code:
client
<?php
function xml_post($xml_request)
{
$url="http://localhost/malakies/server.php?xml=" . urlencode($xml_request);
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result=curl_exec($ch);
if (curl_errno($ch)){
$ERR .= "cURL ERROR: ".curl_errno($ch).": ".curl_error($ch)."\n";
}
return $result;
}
$result=xml_post("Send sth");
echo $result; ?>
and the server code:
<?php
$postdata = $_GET['xml'];
echo $postdata; ?>
All work perfect. But i have a question that it may be a rookie one:)
I want in the server side to have sth like a listener that listens when an http get request have come and do sth with this request. i don't know if http request is the technique that gives me an option like this.. i want sth like that:
while(http request hasn't come yet)
just wait;
do sth with the http request.
Thank you in advance.
PHP script is ran automatically for each separate request. So actually PHP/Apache is already doing what you're asking for.
Maybe this is a bit confusing if you're coming from different programming language (like Java) where you typically have an event loop waiting for new connection.
On the other hand, maybe you had a specific situation in your mind. Please explain your requirements further if that's the case ...
Because your url "ends" in server.php, you need to place a file on your server named "server.php". If your curl script returns a 404 error, you don't have the file at the right location. Where you need to place the file, depends on the operating system. On Linux, this COULD be /var/www/. So you need to find out what your "document root" is. There you would create a subdir malakies. In the Linux example this would be /var/www/malakies/server.php.
PHP will then execute the script inside your file when the request comes in. The data you pass will be placed in an associative array named $_GET. I suggest the following contents for server.php:
<?php
echo "Have a first line so you see something even when no data is passed\n";
var_dump($_GET['xml']);
?>
xml_post in you curl function will then return (disregard the colors)
Have a first line so you see something even when no data is passed
Send sth
If it's not working, what's the error code you get?
I assumed that you have apache installed and that you want to catch the request with PHP.

Categories