getting the POST request from web server - php

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

Related

NodeJS REST API is not accessible from PHP CURL

I have a NodeJS API to send push notifications(both GCM and APN) and its working smoothly.
Now I have an Admin Panel built in PHP Codeigniter and I want to call Push API using cURL. Code goes like this
<?php
$url = "http://my_ip:9100/service/api/push";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
?>
But it fails to connect and output is
Failed to connect to my_ip: Permission denied
I've tested Push API from Postman Client Tool and its working smoothly. If I try this in browser at least it'll enters the API method.
Both PHP Project and NodeJS Application are in same server( NGINX ) and I've tried 'localhost' in place of IP Address but its of no use.
What is that I'm missing here ?
Anything that needs to be added while invoking request in cURL( tried many combinations for curl_setopt() )
Any server setup that is blocking the Request from cURL
Thanks in advance.
You haven't allow CORS from your express server. This should only be the reason that refuses the connection from your curl. See this for allowing cors from express server. CORS on ExpressJS

Curl hangs my apache server when doing POST to https

I have this very weird issue with curl, when I do a post request to my ubuntu apache server the apache server hangs, if I debug my php code I can see that
$fp = curl_exec($ch);
Never returns. I think the actual post request is correct because I can successfully do the same POST request with postman.
Below are my curl options :
$ch = curl_init();
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER , 0);
curl_setopt($ch,CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,$testHeader);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
$fp = curl_exec($ch);
One weird thing is that if im quick, and reload my apache server
sudo service apache reload
while its in this hanged state the curl_exec immediately returns and I get my data. Im not a real web developer and more of a windows guy so any help would be greatly appreciated. :)
UPDATE
I can see no error reporting from Curl since the process hangs. It never comes to the point where it can give me the error. When I reload the apache server the request returns and then there are no errors reported. I added the CURLOPT_VERBOSE option.
UPDATE 2:
OK I can also run the post from curl commandline both from the host and the client. Worth mentioning is that my host is a virtual machine. Beginning to think its more of a network thing.
OK I finally solved this stupid error with the help of this thread
PHP curl exec fail on php script same domain
The solution was to add:
session_write_close();
before the curl_exec
and then
session_start();
After. I hope I can help someone by writing it here.

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.

cURL changing the URL after POST

I am doing an HTTP POST using cURL
$url = "http://localhost:8080/~demo/cgi-bin/execute/100";
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
//execute post
$result = curl_exec($ch);
echo("$result");
//close connection
curl_close($ch);
The post gets executed, but the response is shown with the error:
The requested URL /~demo/100 was
not found on this server.
The above URL, obviously, does not exist not the server because (somehow) cURL has changed the URL.
It should have been /~demo/cgi-bin/execute/100 . This URL works in browser.
Please tell me why does it do that?
AND how can i stop this, for what I want?
Install Fiddler.
Enable debugging.
Visit the site in the browser.
Execute php cURL code.
Fiddler will tell you exactly what the web server is receiving and sending. since you are running locally, you can see exactly what php is sending as well. Compare the two and that will tell you the problem.
Maybe cURL tries to access default http port 80? Try to use
curl_setopt($ch, CURLOPT_PORT, 8080)
It may not be cURL that is changing the URL, rather that the web server is sending a redirect header to cURL, pointing at a different location. Perhaps the following would help:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
where is?
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Categories