how to read an http get request from a php server - php

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.

Related

PHP and Google's reCaptcha v2 - empty response every time

We have a contact.html form that uses reCaptcha v2, whose backend processing is in a php file.
I've taken enough steps to understand that when we send the verification to google's api, the response comes back empty. Below is code that gave me this proof.
$url = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST["g-recaptcha-response"].'&remoteip='.$_SERVER['REMOTE_ADDR'];
$verify = file_get_contents($url);
echo $url;
if (empty($verify)) echo 'Failed to fetch data';`
However, when I manually enter the url into a browser, I get a JSON response back that indicates success.
What, then, is the difference? Why would file_get_contents return empty if a simple get request from a Chrome browser give me trouble?
I have read that file_get_contents is synchronous, so I wouldn't expect this is just a noob error on waiting for the response.
Any help would be appreciated, this is my very first time working with PHP. It's not hard, but I may be missing something vital.
Sorry everyone, I can't understand why, but the problem was in the method used to access the site verify.
Using curl syntax, I finally got it working.
Change the configuration in php.ini file and don't need curl.
allow_url_fopen=0 to allow_url_fopen=1

Can I include a class file from another server

I have a class in a server1.
FILE1 in server1
<?php
class myObject
{
public function __construct()
{
echo 'Hello, World';
}
}
?>
FILE 2 in server2
$section = require('http://xx.xxx.xxx.x/plugins/myObject.php');
$intance = new myObject();
When file2 is called from a php file in server1 itself, Object is created. I can see 'Hello World' in browser.
But it fails when file2 is called from server2. I get fatal error class not found. I have tried include/file_get_contents/read/ __autoload /spl_autoload_register methods also. Nothing helps to invoke my class from another server.
Is this possible? Can anyone please suggest an alternative? Please help
UPDATE:
i have fopen and include url on in server2 from where iam trying to include file. Actually I needed the class and my website to be two servers.
SCENARIO:
I am tring to build a wallet website in server2. I have necessary plugins in another server [s1]. I have written a class file interacting with plugins in server 1 itself. Iam planning to have wallet websites in more servers. but all of these websites will interact with class in server1. If I could somehow get the code in that class to my website, then i could create objects and call class methods from other servers also. Please suggest other way to implement this.
UPDATE 2:
Can I build somthing like API where all my websites will send request to main class in S1 and get get response. An example would be helpful
Is this possible?
No. PHP code never leaves the server. That's why there are dependency management tools like Composer.
If you want to run code on server 2 from server 1 you need to implement a webservice that does that. So server 2 "calls" the php file on server 1 it does not "require" it. Try something like this:
File 1 Server 1
<?php
class myObject
{
public function __construct()
{
echo json_encode(['result'=>'really cool data result'])
}
}
new myObject();
?>
File 2 Server 2:
<?php
set_time_limit(0);
$url = "http://xx.xxx.xxx.x/plugins/myObject.php";
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result_json=curl_exec($ch);
// Closing
curl_close($ch);
$result_obj = json_decode($result_json);
$result = $result_obj->result
If you want to run code on server 1 you have to do it like this and then use the result from server 1 and do something with it on server 2. You cannot run code on server 2 using software that exists on server 1 because by definition you cannot run something on one machine that exists on another machine.
You may need to first download the file and then requireing it.
No you can't do that. And even if there are really bad work arounds to do that, you should NEVER do that.
It makes your code very vulnerable, if you care about security.
Can you require from remote file? No. Can you include from remote file? Yes. This is known as Remote File Inclusion and usually it is considered a security risk. From PHP's documentation of include:
If "URL include wrappers" are enabled in PHP, you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Supported Protocols and Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.
To use this feature, open your php.ini file and set the allow_url_include as 1 or "On".
After that, you can now do
$section = include('http://xx.xxx.xxx.x/plugins/myObject.php');
$intance = new myObject();
Be warned though, if you allow a user to manipulate the argument to include, he would be able to inject arbitrary PHP code.

how to run PHP in a Angualr2 CLI

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?

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

php curl for facebook post tweet

I want post tweets into facebook using php curl , this is my snippet I used for posting tweet into FB - FB CURL SNIPPET
But i am not find any updated tweet in my facebook,
am not sure but i thing somthing goes wrong,
Can you tell me, snippet is correct one or not?
Thanks
This calls for debugging.
First port of call: It could be that the cookies are not saved: Check whether the script actually generates a my_cookies.txt file. If it doesn't, create an empty one and do a chmod 777 on it.
Second port of call: curl_error().
Replace every curl_exec() call in the snippet by this:
$success = curl_exec(....... your options .....);
if (!$success) echo "CURL Error: ".curl_error();
this might give you some pointers as to what goes wrong.
However, seeing as the script tries to imitate a browser instead of using an API, it could be that the structure of the submission form has changed on Facebooks's side, in which case you'll have to parse the output cURL gives you and see what goes wrong.
All in all, if there is any way to do this cleanly through an API - I don't know whether there is - it would be much preferable to this.

Categories