Writing a curl proxy under linux - php

Hello
I am working with a legacy system where an ASP.NET application posts an XML file to a server via curl.exe (this url to send is configurable by a .config file).
Now due to legacy system limitations, I need curl post this XML to my ubuntu server by changing the said .congfig file, modify the received XML as I need and finally curl post it to the real server.
How can this be done ? My guess is a php or a python script running under apache2 server, listening posts. Once received the xml file, do the required modifications on the file and post to the real curl server.
Via php or python, how can this be done ?

Since ASP.NET application is posting XML, you simply need to handle a normal POST request, modify XML to match your requirement and post it using cURL to the real cURL server. In PHP, it would look something like this (more or less meta code, error checking and additional logic is needed):
$xml = $_POST['xml'];
// do something with posted XML
.....
// post it to the "real" cURL server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('xml' => $xml));
$result = curl_exec($ch);
curl_close($ch);
That's about it, check cURL documentation and use what is necessary for POST to work with your server, and your are all good.

Related

Print sent php curl request

How can I print request I have sent, header and body? Following:
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
gives me just $r['request_headers']?
i made a server & website specifically for this purpose, http://dumpinput.ratma.net for the website, and https://github.com/divinity76/dumpinput.ratma.net for the server, note that it does not support httpS, so if the input is sensitive, you should probably run your own instance of the server (it's free & open source~), also note that i run the server on a dev-vm from cloudatcost.com, which has a bad reputation for uptime, so don't depend on the website being up.
you could also set up a netcat server as the target of the curl request (but that won't work with Expect 100 Continue POST-requests, but the dumpinput server will still work.)

Communication between two scripts

i'm creating embedded system with infinite Python script (launched on startup) and PHP web page. PHP web page have to communicate with that script.
Current solution is file based communication. PHP writes to file some command and Python writes response to some other (or same) file.
Another possible solution is to call some NOT infinite Python script only at need with PHP $response = shell_exec('./script.py');
Both solutions are possible but they are complicated and I need that Python script to be infinite.
Is there any way how to open some communication tunnel between two independents scipts on same linux device?
Like UART, telnet, etc. between two devices.
Now i'm searching for solution with PHP-PYTHON and PYTHON-PYTHON communication but sometimes i need this for example with Bash, TCL, etc.
Thank you (and sorry for not good english)
Radim
Without knowing much about what you are trying to achieve, i would go for a base http solution, starting a simple http server with python
import SimpleHTTPServer
import SocketServer
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
.... do something with the http request and send the response
Then the php script can simply make some GET/POST/etc.. request to python directly, and grab the http responses, for example via cURL:
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "localhost");
// set port
curl_setopt($ch, CURLOPT_PORT, 8000);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
Of course this is a simple stub and it should be tweaked a bit, but it should give you a starting point.

PHP - Retrieve Facebook image in Google Compute Engine

I've tried to retrieve the image data of my Facebook profile picture, using both file_get_contents and curl.
The problem occurs on my Google compute engine instance, while on any other server (localhost - mamp, AWS) the script works fine.
An example for one of the scripts I was using
<?php
var_dump(
json_decode(
file_get_contents("https://graph.facebook.com/_FACEBOOK_ID_/picture?width=720&height=720")
)
);
Please keep in mind that I've tried using the parameter redirect=false, and accessing the image url I've got in my json response returned false as-well.
Also, I've tried using wget in SSH to the image's url, which returned (403) Forbidden.
My assumption is that I need to configure something differently in my server, not PHP, but because I'm able to retrieve any other image, with the same script - I'm not sure what.
I've already experienced this exact problem,
Ignoring SSL verification while using cURL did the trick for me.
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Ignore SSL verification
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/_FACEBOOK_ID_/picture?width=720&height=720");
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);

PHP Send Custom HTTP Request

How can I send a custom HTTP Request to a server whose URL is "http://[IP]:[Port]/"?
What I mean is that, instead of the first line being a normal GET or POST like so:
GET /index.html HTTP/1.1
Host: www.example.com
How can this be replaced with something just like:
CUSTOM
Host: [IP]
I don't mind having to use any additional libraries if necessary such as cURL.
UPDATE:
I've tried using the following cURL code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://[IP]:[Port]/");
curl_setopt($ch, CURLOPT_PORT, [Port]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "CUSTOM");
$output = curl_exec($ch);
curl_close($ch);
print($output);
But it just keeps loading for 2 minutes until it said internal error (both with and without using the CURLOPT_CUSTOMREQUEST). However, if I use a standard website such as http://google.com it'll work fine.
Also I forgot to mention, the port my server is using is 7899, is this ok? Open it in my web browser fine, but cURL doesn't seem to be able to.
Looks like there's nothing wrong with your code. If you're using a shared hosting provider, ask them to open up outbound TCP port 7899.
You can create a custom HTTP request method using the http_request_method_register function:
http://www.php.net/manual/en/function.http-request-method-register.php
This code needs to be run on the server that will be handling the request. If you try to use a non-standard HTTP request method on any old server, depending on the server it may ignore it, or may return an error code.

how to capture CURL / XML data with Apache?

I'm developing a gateway script that needs to send info to another provider's server, and I need to debug the code.
Is there a way, on my own Linux + Apache + PHP server to capture the CURL / XML data from this script?
I know with PHP, that I could see for example the $_POST, $_GET or $_REQUEST data in a script, but with CURL I don't actually get to the http://intranet/capture.php script in my browser - so this doesn't work.
Is there any other way, with a script on the server to capture everything that's passed to the server, and dump it to a database / flat file?
I even tried monitoring /var/logs/http/access_log on the Linux server, but it didn't reveal much
So, how can I see what the CURL script does, exactly, as the server sees it?
what you can try is this.
echo htmlentities(file_get_contents('http://intranet/capture.php'));
I'm not sure if this is what you mean but it does the same as curl (sort of)
You want to see the output of curl
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url ); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
$result = curl_exec($ch); // run the whole process
curl_close($ch);
echo htmlentities($result);
I hope this is what you mean
In this case, you are the client and the provider's server is the server.
Assuming you are running the curl command from the client, all you can get to is what Robert Cabri said.
If you are attempting to look at whats being received by the server, you need to have appropriate access and also need to know what application stack the server is running to serve your request.

Categories