request pool for php curl - php

My PHP server (A) needs to make an http request to another server (B) each time it's called.
I'm familiar with the usual use case for using curl in PHP
$ch = curl_init();
curl_setopt_array($ch, $curlOptions);
$responseBody = curl_exec($ch);
$curlErrorCode = curl_errno($ch);
curl_close($ch);
However I'm calling the same server (B) every time. Is there a way to keep the connections between A and B open (keepalive)? Maybe some sort of connection pool?

Implementing socket handling will be great if you want two servers to communicate with each other.
http://php.net/manual/en/sockets.examples.php

Related

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: How to use cURL to interact back and forth with hardware device?

I'm familiar with cURL for retrieving files, submitting POSTs, etc, but now I need a php script to "chat" with a networked hardware device. This device has a kind of Terminal Mode and is accessible through a shell with tools like netcat. I'm trying to reproduce that dialog with a php script.
So far I can open the connection and get the password request:
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, 'http://192.168.0.35:4025');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
echo "DSC: $result \n";
// submit password
// read answer to see if password is approved
// submit command
// get command answer
curl_close($ch);
How can I now submit the password as a single string, over that existing cURL connection, and keep my script interacting back and forth sending and receiving strings?
Is there a better tool than cURL for this?
The plan is to keep the connection alive forever, for monitoring purposes.
Any assistance will be appreciated.
you can use socket check this
Socket : sockets are the fundamental "things" behind any kind of network communications done by your computer. For example when you type www.google.com in your web browser, it opens a socket and connects to google.com to fetch the page and show it to you. Same with any chat client like gtalk or skype. Any network communication goes through a socket.

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.

Writing a curl proxy under linux

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.

getting the POST request from web server

I'm having troubles debugging a POST request I'm making from my web server to another web server.
I'm trying to communicate with a SOAP web service but from some reason a code that worked well from local machine fails when executing on my server
Looking for a way to see the post request my server make to the web service server
web server OS - CentOs
using PHP curl to make the request
Ideas anyone?
Wireshark? If you've got to connect to the remote end using SSL, then run a stunnel client on the soap client and route requests through that tapping in between.
I had the same problem, and using CURLINFO_HEADER_OUT made outgoing request headers show up in debug info.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data = curl_exec($ch);
var_dump($data);
$details = curl_getinfo($ch);
var_dump($details);
To see the POST on the server
$h=fopen('out.txt','w');
fwrite($h,var_export($_POST,true));
Maybe the curl is disabled on your server
Or point your client at:
<?php print_r($_POST); ?>
C.
Redirect the post request to a server you control. You can then read the posted data using echo file_get_contents('php://input');

Categories