Read PHP file containing XML with POST request - php

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?

Related

Parsing - can't get data from PHP file

I'm trying to parse data from http://skytech.si/
I looked around a bit and I find out that the site uses http://skytech.si/skytechsys/data.php?c=tabela to show data. When I open this file in my browser I get nothing. Is the file protected and can run only from server side or something?
Is there any way to get data from it? If I cold get HTML data (perhaps in a table?) I would probably know how to parse it.
If not, would it be still possible to parse website and how?
I had a look at the requests made;
http://skytech.si/skytechsys/?c=graf&l=bf0b3c12e9b2c2d65bd5ae8925886b57
http://skytech.si/skytechsys/?c=tabela
Forbidden
You don't have permission to access /skytechsys/ on this server.
This website doesn't allow 'outside' GET requests. You could try parsing the data via file-put-contents but I don't think you will be able to get specific data tables (aside from those on that home) due to AJAX requests that need to be made. I believe the /data? is the controller to handle data which is not exposed via the API.
When you open this URL in your browser you send GET request. Data returned under this address is accessible after sending POST request with params as follows c:tabela, l:undefined, x:undefined. Analyze headers next time and look on Network log if you are using Chrome/Chromium.
If that website does not expose an API, it is not recommended to parse the data, as their HTML structure is prone to change.
See:
http://php.net/manual/en/function.file-put-contents.php
And then you can interpret it with an HTML-parsing engine or with an regular expression (not recommended).

file_get_contents doesn't work as well as direct string?

Its seems like file_get_contents is not working exactly returning a true string value, otherwise it's different.
For example i'm working on a rest API to which I have to send contents via json. One of theses contents is a xml file content.
When I put the xml straight as a var's value, striping slashes, it works. But if I do a file_get_contents() on a file containing the exact same xml (with or without slashes), It doesn't.
Has someone already seen this happening ? If yes, does some one know how I handle this ?
Thanks
To answer my own question :
Actually the probleme was that when I did file_get_contents() on a xml file, it returned html encoded characters when I echoed it.
But if, instead of doing so, I just putted a $var = "<my xml definition>"; and echoed it as well, the encoding was not the same. Which made me think that using file_get_contents or not made a really big difference when trying to make REST requests.
I precise that I was trying to pass via ajax to php, xml definition using json.
Actually it was the api which was not working, because of maintainance. And as they didn't communicate on that I was thinking that I was wrong and that my code had mistakes.
So to conclude : file_get_contents() seems to work just fine and as expected.
I hope this clear and long answer wont knockdown my reputation this time :)

Get a JavaScript variable from a remote page?

I am currently working on developing an API for a company. Specifically, here's my issue. They have JavaScript arrays on a webpage that their webmaster updates. I have to pull these arrays into either a simple JS script or PHP file and get the contents of these arrays, which I can then arrange according to the API's specifications and output it as JSON.
How do I pull a JavaScript variable in from a remote page in either PHP or jQuery/JS and make it usable for other applications?
No, I don't have access to the company's website. I have to work off of page scraping for this one.
Thank you!
You can't access private javascript variables remotely, due to Same origin policy. They would have to output the arrays in some kind of readable format that you could access using AJAX, probably as JSON.
Edit: As mentioned below, if the array is explicitly defined as text in a javascript file, you could grab the contents of that file using cURL in PHP
If I where you, I’d use PHP to file_get_contents (or CURL depending on the server config) the page and then parse it based on whatever the markers are to find the value of the variable, assuming it’s written out to the page in the first place.

How to use function call in external PHP url

I understand that within same folder, I can use include() function for external PHP file, but now I would like to call the function in another PHP file which located in another URL.
For example, my live website (liveexample.com/table.php) has drop-down list and table, but without data.
My another PHP file (dataexample.com/data.php) is connected to database and process to extracting data out. But, it is in another server.
I need to make my data on [dataexample.com/data.php] delivers to [liveexample.com/table.php] and let the looping to draw table with data out on [liveexample.com/table.php] page.
Anyone has idea to design this method of delivering data from another server to another by using function call in PHP?
Or any other better solution to deliver my data between two different servers such as make the data record set into array and send to [liveexample.com/table.php]?
Please give me advise or consultation. Appreciate much!
I think SOAP webservice would be perfect for you to attain what you want but if possible just copy the same codes you have from the separate server.
If you make [dataexample.com/data.php] output your data as XML, then you can use it as a web service. What that means is, you can take that XML output (by sending a request the the data URL), and then parse it to load the data. This way, you can use that service any way you want. One way would be like you wanted, other examples would be via AJAX, or Flash etc.
So here are a few topics worth looking into:
using PHP for web services: http://wso2.org/library/3032
parsing XML data: http://www.w3schools.com/php/php_xml_simplexml.asp
I hope this will give a pretty good idea of how to achieve what you want to accomplish, because there a few options you can go by. Like Cristopher said, SOAP is one of them.
Have a great day.

Passing Information Between a Database through JSON

I need to display information from an SQL database which resides on a server, to a remote webworks mobile device. I am extremely new to passing information from a server so bear with me. My normal understanding is that I would have an HTML file that accesses a php script which then itself connects to the database and displays the information.
However, in webworks the HTML/Javascript files reside on the device and are separated from any php file so I need a method to communicate to get the data from the database. I have looked through JSON and read all the tutorials on w3schools and I understand the syntax but I don't understand how to use it. How could it connect to a database? My aim is to simply display the table entries on a mobile device app running HTML5 webworks. Again I am very new to this so any explanation would be very helpful.
Chances are, you should get a book. This is not something that can be explained in detail in a short answer on this site.
in summary however, you can either
1) send requests to your php script by submitting a form on an html page, which will load a new page filled with whatever PHP sends back. in this case you do not need to use JSON at all as PHP would be returning a full html page.
2) you can use AJAX. AJAX is a javascript method of sending requests to the server (PHP), and getting a response without ever loading a new page. you would use AJAX to send a request to the php page, the php page would access the database and send back a response, the javascript would then take the response and do whatever it needs with it. the response data is usually formatted in a JSON format, because PHP can easily create JSON, and javascript can easily decode JSON once it receives it as a response. to make using AJAX simpler, you may want to look in to using jQuery, a javascript library which can simplify the process.

Categories