Is PUT similar to POST?
I'm getting some inbound requests (apache) with this:
[REQUEST_METHOD] => PUT
I've never worked with this request method before. So I have to ask if I'm supposed to process it differently.
The people sending me data are claiming to be sending xml. So my script has this:
<?php
if(isset($HTTP_RAW_POST_DATA)) {
mail("me#myemail.com","some title i want", print_r($HTTP_RAW_POST_DATA, true));
}else{
die("not post data");
}
?>
I'm stuck here now. If there's a PUT request, do I replace $HTTP_RAW_POST_DATA with something else?
According to the php docs, PUT data can be read using the php://input stream (which is preferred over $HTTP_RAW_POST_DATA).
$putdata = fopen("php://input", "r");
$str = stream_get_contents($putdata);
fclose($putdata);
Related
I am trying to build a post server similar to posttestserver.com and have been runnning into lots of trouble.
The following returns nothing -
do {
$data = file_get_contents('php://input');
} while (empty($data));
header('HTTP/1.0 200 OK');
header('Content-Type: text/html');
var_dump($data);
I have also had a look into the use of sockets but the client should be directed to a URL rather than an ip/port for the clients ease. I suspect that this is what i need to use but am not sure where to start.
For what its worth, the client expects an HTTP 2XX response code from its HTTP POST request, and the client will not attempt submitting the next HTTP POST request while a previous request is still in flight.
Any ideas?
It would seem that you cannot capture and view the POST data in the one browser window.
For what its worth, here is the code that worked in the end -
$data = file_get_contents('php://input');
//do something with the data such as write to file or database
Then you could use the data in another PHP script.
I have an internal server that is generating JSON every few minutes. I need to pass this to and external server so I can manipulate the data and then present it in web interface.
Here is my python that send the data to PHP script:
x = json.dumps(data)
print '\nHTTP Response'
headers = {"Content-type": "application/json"}
conn = httplib.HTTPConnection("myurl.com")
conn.request("POST", "/recieve/recieve.php", x, headers)
response = conn.getresponse()
text = response.read()
print "Response status: ",response.status,"\n",text
conn.close()
and Here is my PHP to receive the data
$data = file_get_contents('php://input');
$objJson = json_decode($data);
print_r ($objJson);
My python script return with response status 200 which is good and it returns my JSON. But on the PHP side I want to be able to store this information to manipulate and then have a web client grab at it. However it appears that even if I say
print_r ($objJson);
when I visit the .php page it does not print out my object. I suppose the data is gone because file::input will read only and then end?
Don't use file_get_contents()
Just:
if($_POST){ echo "WE got the data"; }
and print_r will help you with where to go from there, if you are unfamiliar with PHP arrays.
Try to use named parameter containing your JSON data while sending data to PHP and try to use the $_POST superglobal array in PHP (because you obviously connecting via cgi or similar interface not via cli).
You can see all the POST data by printing your $_POST array:
print_r($_POST);
Here is what I did in the end as a quick fix
the python
x = json.dumps(data)
headers = {"Content-type": "application/json"}
conn = httplib.HTTPConnection("myurl.com")
conn.request("POST", "/script.php", x, headers)
response = conn.getresponse()
text = response.read()
print "Response status: ",response.status,"\n",text
conn.close()
then I realized that the PHP was receiving the post data but I was having a hard time manipulating it. So I just sent the JSON to a receiving PHP file that wrote the data as file and then has another JavaScript request grab that.
the php
if($_POST){ echo "WE got the data\n"; } //thanks rm-vanda
$data = file_get_contents('php://input');
$file = 'new.json';
$handle = fopen($file, 'a');
fwrite($handle, $data);
fclose($handle);
exit;
Then from my client just a ajax request for the file and parsed it on the client side.
Eventually I have to rethink this but it works for now. I also have to rewrite over the new.json file each time new data comes in or parse the old data out on the JavaScript success callback. Depends what you want to do.
Is there any way I can access data that was sent via HTTP PUT method other than $putdata = fopen("php://input", "r");?
I have never worked with PUT and DELETE methods and $putdata = fopen("php://input", "r"); seems a bit sketchy. Will it work everywhere is a specific server/php.ini configuration required?
I know that I can get the request method from $_SERVER['REQUEST_METHOD'];
But will the data be in $_REQUEST, if yes then what php://input is about?
And how do I access data that was sent via DELETE?
No, you will need to parse the request manually. $_REQUEST only contains data coming from GET and POST requests; for everything else you are on your own.
If your HTTP request has Content-Type: application/x-www-form-urlencoded, you can parse it back into a variables array very easily with parse_str like this:
parse_str(file_get_contents('php://input'), $vars);
print_r($vars);
You can use this content type with any HTTP method, there is no standard-imposed limitation.
I've looked everywhere but can't find what I need so hence why I'm posting this question.
I'm looking for code for a PHP proxy to help me effectively do cross-domain jquery requests but have these requirements:
Must be PHP
Cannot use cURL -- my server config doesn't support it. Presumably this leaves fopen() but I'm open to alternatives.
Must support both GET and POST requests
Support both responses for either XML and JSON
I'e searched high and low for a solution but always find solutions that use cURL or doesn't support POST.
I'm currently using this which doesn't work for POST:
header('Content-type: application/json');
$daurl = $_GET['url'];
$handle = fopen($daurl, "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}
file_get_contents(). See Example #4 at the linked page (PHP online docs) on how to do 'custom' requests using streams for POST and arbitrary HTTP headers.
in other for you to get quick answer, you first need to do some work yourself, then when you get stuck, you can post it here.
The question is too broad to be answered. you are requesting for a complete solution, you won't get it
OK n00b here with SOAP,
Would like some clarification on how to use SOAP.
Question:
I have a Java JSP that posts a WSDL (Looks like XML format) to my PHP script, but how do I get this in the PHP script? The URL for the WSDL will be different every time.
I'm sure it's very simple but just don't see how or am I not understanding this correctly?
You can try something like this:
try {
if (!($xml = file_get_contents('php://input'))) {
throw new Exception('Could not read POST data.');
}
} catch (Exception $e) {
print('Did not successfully process HTTP request: '.$e->getMessage());
exit;
}
This will read the body of the POST request to the $xml variable and print an error if there is one.
Do you mean that the JSP sends the WSDL in a POST request to the PHP script?
If so, have a look at the $_POST array. If you specify exactly how the JSP sends it, I can probably help you more.
Anyway, once you have the WSDL url in a variable in your PHP script, you can have at it with the SoapClient class.
Assuming the best scenario:
$soapClient = new SoapClient($wsdlUrl, $soapOptions);
$soapClient->callYourMethod();
But you're likely to hit a lot of brick walls when using SOAP. Here's the documentation for SoapClient.
Edit:
So, the WSDL is POST-ed. Then, you could access it either by using $HTTP_RAW_POST_DATA if the XML string was sent as the HTTP body, or by using the $_FILES superglobal if the XML string was send as a part of a multipart request.
Something like this:
$wsdl = $HTTP_RAW_POST_DATA;
$wsdlUrl = 'data:text/xml;base64,' . base64_encode($wsdl);
$soapClient = new SoapClient($wsdlUrl);
Anyway, $HTTP_RAW_POST_DATA is only available if the php.ini setting always_populate_raw_post_data is turned on. Also, if the request was multipart, this setting is ignored, $HTTP_RAW_POST_DATA is not populated but you get access to the posted parts using $_FILES. And you may, indeed, use php://input instead of $HTTP_RAW_POST_DATA.
Also, data URIs may only be used when allow_url_fopen is turned on in php.ini.