file_get_contents doesn't work as well as direct string? - php

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 :)

Related

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?

base 64 encode a form file

I'm writing a very simple file sharing site in JS and PHP. I've got a drag/drop working, so the browser gets a file object upon drop, and from there I tried to send in a xhr request to an upload page. However, I can't seem to just drop a binary file object in a request header, and so was wondering how I'd go about base64 encoding it.
In PHP I'd use base64_encode, but I'm not even at the PHP page yet. Maybe you could suggest an alternative method to my current one?
Oh, and in the PHP that receives it, it writes to a semi-random file after base64_decodeing the file.
EDIT: I worked around it, but there isn't really a good answer. Thanks for helping!
Here's my demo: http://bernsteinbear.com/up
There is a function in the works that is currently only supported in Firefox, xhr.sendAsBinary, but for now you can do the Base64 encoding in Javascript with this custom function:
http://www.webtoolkit.info/javascript-base64.html
Alternatively, you can implement sendAsBinary yourself, as seen here:
http://hublog.hubmed.org/archives/001941.html
Just be aware that the Base64 method is currently the most compatible method.
Is there any reason you aren't using something like the Valumns File Uploder? I don't know why you're wanting to add a binary file "as a request header" (that makes little sense to me), but having to base64 encode it before sending seems a bit silly when HTTP can handle sending binary data in both directions quite easily (example with forms). Then again, I'm unfamiliar with the File API, so I'm not sure what special things you might be doing with it (are you transforming the file at all before sending?). Maybe I'm missing the point of this exercise.

parsing \\xc3\\xb6 from a url

I'm trying to support an API, it's a remote server pushing data to me. When passing data, reading directly from $_GET['value'] I get strings with this in them:
\xc3\xb6
They also needed a very specific url structure, which I've had to use curl to work around. When using curl on the api I get this instead:
\xc3\xb6
Is the problem on my side or on his? How can I decode this reliably? Is there a better way than using curl available? (If neccesary I'll recode that part of it to skip curl, I just wanna get it working at this point).
//edit : Note that I'm writing the $_GET['value'] directly to a file to test. And it looks the same.

Anyone able to read ning's json export files using PHP

I have a client's JSON files that he got from the NING exporter. I'm trying to load the data into PHP but seems like the json isnt properly formatted or something. SO PHP is not able to parse the JSON. I also used another PHP class to do it but that did not work either. Below is the content of one of the files
([{"id":"2492571:Note:75","contributorName":"16szgsc36qg2k","title":"Notes Home","description":"Welcome! To view all notes.","createdDate":"2008-11-14T08:44:58.821Z","updatedDate":"2008-11-14T08:44:58.821Z"}])
Help appreciated!
The parens at the beginning and end are not valid in JSON. It should parse after stripping those.
The JSON file from NING exporter is not properly formatted. For some reason, some commas are missing and you have '}{' pattern, instead of '},{' and the first and the last char is not correct.
You can write a small routine to pre-parse the file and fix those problems and some others that might appear or you can take a look at the code of this Wordpress plugin http://wordpress.org/extend/plugins/import-from-ning/ and copy the routine that fix the json file.
If you'd like to move your Ning data to another platform, you could consider Discourse. There is already an importer for it.
If you don't want to use Discourse, you can still use the (Ruby) importer source code to see how to parse the JSON file.

Passing variable to Google Charts URL

This is probably something really simple, however I am quite new to PHP, and havent done any HTML in years.
I need to get a PHP variable filled with an array of figures into Google Charts. My code for this so far is:
<img src="http://chart.apis.google.com/chart?
&chs=340x175
&chd=t:<?=$filedetail[1]?>
&cht=lc
&chtt=Test
">
However, Google reports an error, as it stops at the ?=$filedetail[1] for some reason. It doesnt seem that reading the variable is the problem, more that the API simply cant read past the start of the PHP tags.
Thanks,
Rob A.
EDIT: I have managed to make Google accept the URL, however now it is not showing anything on the chart, as its filling in the &chd=t: field with instead of the figures within that variable.
The URL reads like this:
http://chart.apis.google.com/chart?&chs=340x175&chd=t:%3C?=$filedetail[1]?%3E&cht=lc&chtt=Test
If oyu say Google is complaining about the ?=$filedetail, chances are you are doing this in a file that is not being parsed by PHP, for example a file that ends with .html or .htm.
You can see whether this is the case by looking into the page's source code in the browser. If you see the PHP command in the source as you wrote it above, the PHP code was never executed.
The easiest way to fix that, if that's the problem, would be to switch to a .php file extension.
In URLs, literal & should be written as &
Edit: And you can't do ?&chs -- it should be ?chs. The line breaks are probably going to break the URL too...

Categories