Cannot see JSON posted from ASP.NET/IIS Application - php

I am working with another company to build a customer billing interface to their reporting software. Their system is built using ASP.NET/IIS and my system is built using PHP/Apache. They have an API that sends/receives JSON data over POST using a content-type of application/json; charset=UTF-8
I wrote a wrapper for sending data to them which they receive just fine, and also set up an endpoint for them to send data to which works just fine when I test it with cURL or Chrome's "Postman" extension, but I can't see any data they send me.
Basically my endpoint grabs the raw post data from php://input like this:
$pData = file_get_contents('php://input');
$pData = json_decode($pData);
As stated before this works great when I use postman, but when they send the request I see no raw data, nothing in $_POST and of course nothing in $_GET. I have even tried $HTTP_RAW_POST_DATA which gives me the same result as php://input.
I am logging all requests and can see they are making the request to my endpoint, but there is no content-length given so I can't tell if the data is even making it to me. Is there something I am missing on my end that would cause me to miss the raw data from an ASP.NET application? Or is it safe to assume the issue is on their end?
Thanks

Issue was on their end apparently. index.php was under a couple directories like https://www.domain.com/dir1/dir2/index.php - They were posting to https://www.domain.com/dir1/dir2 which apparently didn't work but posting to https://www.domain.com/dir1/dir2/ worked fine. Not sure why that was an issue for ASP.NET/IIS and not for PHP/Apache, cURL from a linux terminal, Postman, and http://hurl.it

Related

Parsing raw POST XML into a text field

I need to store XML data sent over HTTP POST to my server. In the log files I see that the data is successfully sent to my server. But I have no idea how to get the data.
I tried to catch them with the php://input stream like in the code below. The problem I see is that php://input is just read when the file containing the code is called.
$xml = file_get_contents("php://input");
$var_str = var_export($xml, true);
file_put_contents('api-test/test.txt', $var_str);
Is there any way to set some kind of listener/watcher to the php://input stream? Maybe PHP is the wrong technology to realize this. Is there some other way like AJAX?
The problem I see is that php://input is just read when the file containing the code is called.
Yes.
That's how PHP (in a server-side programming context) works.
The client makes an HTTP request to a URL
The server receives the HTTP request and determines that that URL is handled by a particular PHP program (typically by matching the path component of the URL to a directory and file name unless the Front Controller Pattern is being used)
The PHP program is executed and has access to data from the request
The server sends the output of the PHP program back
Is there any way to set some kind of listener/watcher to the php://input stream?
You get a new stream every time a request is made. So the typical way to watch it is to put a PHP script at the URL that the request is being made to.
Then make sure each request is made to the same URL.
(If you need to support requests being made to different URLs, then look into the Front Controller Pattern).
Maybe PHP is the wrong technology to realize this.
It's a perfectly acceptable technology for handling HTTP requests.
Is there some other way like AJAX?
Ajax is a buzzword meaning "Make an HTTP request with JavaScript". Since you are receiving the requests and not making them, Ajax isn't helpful.

PHP Is a redirect in API that returns JSON okay?

My API written in PHP returns JSON in an api used by a phone.
Depending on certain paramaters, the PHP file either returns the JSON itself or redirects to a different PHP file that returns similar JSON.
In a browser, the JSON returned directly from the starting file looks identical in form to the JSON returned after the redirect. However, the phone is not getting any JSON at all if there is a redirect.
This doesn't make sense to me but it's what I'm seeing. Is it possible for a redirect to mess up a JSON response?
For the record, here is what the JSON looks like in the browser:
{"comment":[{"response":"Hello World"}]}
Edit:
Apparently, redirects in restful APIs are somewhat frowned upon but possible, however, do you have to do something with a status code? This is unfamiliar territory for me.
JSON response redirect
Thanks for any ideas.
You can do redirects using 307 temporary and 308 permanently codes as stated in the IETF draft https://tools.ietf.org/id/draft-hunt-http-rest-redirect-00.html#rfc.section.2.1
Redirection is not the right tool in your case.
As I understand you‘re trying to delegate different endpoints to different files. You should have a look at request routing which happens server side, the client should not have to deal with this.
https://link.medium.com/Sdp3DyXTHX
https://symfony.com/doc/current/components/routing.html
https://www.php-fig.org/psr/psr-7/

Read PHP file containing XML with POST request

I'm strugling with this for a couple of days now. I read articles and other stuff and I can't find a solution. So.
I'm working on a ski resort website based on Wordpress which has to read data from ski conditions website. The owners of the API are sending me XML data via URL: http://snezni-telefon.si/svrh_xml_test.php and they are telling me to read it with POST request. I tried everything from file_get_contents to simplexml requests and nothing works. So my question is:
How to read XML data generated in PHP file from another server with $_POST request?
Thank you!
EDIT: I tried simplexml_load_file, wp_remote_post and file_get_contents.
EDIT #2: So after going back and forth with my client they told me everything that they are doing with the script. So they have a script which contains and XML in string form, which is calling my script in which I have to get $_POST['XML'] variable. So if I understand correctly I just have to parse that $_POST variable which is simple and logical but I can't seem to make it work. Any suggestions?

Explain file_get_contents('php://input')

I am writing an android app to connect to PHP web service and through my searches in internet I faced file_get_contents('php://input'); and I understood some parts of it's functionality but I still don't get it. What are php://input or php://stdin or stuff like that?
I've read http://php.net/manual/en/function.file-get-contents.php and I confused much more.
Please explain it completely.
The information comes from here: http://www.php.net/manual/en/wrappers.php.php
When information is sent to the server via a POST request, it is saved in a temporary file.
The command file_get_contents('php://input') reads the raw information sent to PHP -- unprocessed before it ever gets put into $_POST or $_REQUEST super globals.
This technique is often used when someone is uploading a file, such as an image.
EDIT: removed $_GET

JSON is not Retrieved When Put on the Server but Does Work on Localhost using XAMPP

I'm having a problem, my code works in XAMPP. However, it is not working once I put it in the server, I host it with fatcow, it seems that they work json and php 5.2.
I'm using Jquery and Json, two commands I'm using to retrive the data $.getJSON and $.post, I'm using two libraries
src="http://code.jquery.com/jquery-latest.js" type="text/javascript">
jquery.json.js
I have no clue what is the problem, the json encode outputs json data, but my code from js doesn't retrive it doesn't seem that it copies to the array i have with $.getJson. I dont understand, all help will be greatly appretiated.
Looks like your JSON data is not returned due to the Cross Origin Resource Sharing policy (CORS).
You can solve this problem by:
Putting the JSON file in the same domain as the Javascript/HTML file that requests it
OR
By adding the header:
Access-Control-Allow-Origin: *
to your JSON file.
More information on how to add CORS headers
More information on CORS
Hope that helps :)

Categories