Anyone able to read ning's json export files using PHP - 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.

Related

Sublime text beautify PHP serialized data

I deal with serialized PHP data a lot (wp developer) and I'd like to know if anyone out there has figured out how to beautify serialized php data from within ST3. I'd like to be able to view my serialized php data in a nice indented format in the same way that I can already view my JSON and other serialized data formats. There are plenty of tools online that do this, but i'd like on integrated into ST3.
There is a PHP beautifier out there which i believe runs the PHP_Beautifier PEAR package, but it does nothing to serialized data (probably because the extra whitespace would break the format).
I have had relative luck with copying the data to a new file and running a regex replace /(.*?);(.*?);/$1 : $2,/ and then running Format as Javascript from CodeFormatter. but obviously this quite a bit of work to do each time.
I'm new to ST3 so I'd like some opinions on what my best option is here. Is there a solution out there or should I create a plugin to send the string to an online beautifier, or write a macro or something?
Cheers
I am afraid that PHP serialized data cannot be formatted (like JSON or XML)
The PHP serialization format does not allow additional white-space between the serialization token. The only thing you can do is to unserialize the data for display and then re-serialize it before saving the file.

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

Creating an actual XML RSS feed in PHP

I currently have a dynamic RSS feed that looks like this.
This is fine for importing into feed readers, but now I need one that can work with my jQuery plugin. As a result, I'm trying to create something that looks like this.
I may be wrong, but it seems that the only difference is that former is a PHP file where the second is actual XML.
How would I go about creating the XML version?
You would need to change the content type headers to "application/xml". In php, this is done by
header('Content-type: application/xml');
It seems somewhere your scripts is actually setting the headers to "Content-Type:application/rss+xml;" so you may only need to replace this line.
I'm not sure why this would actually matter to your jquery plugin, but at the very least, the example you gave uses the same headers.

Extracting Data From Remote XML & Creating Wordpress Post

I've been searching for some time to figure out how to extract data from a remote XML file and then create a post automatically with the parsed XML data. I have figured out the functions to create a post using cURL/PHP but I'm not sure how to pull data from an XML file, put that data into strings and then apply those strings to a newly created post. Also dupe protection would be nice.
If anybody knows a good starting point for me to learn or has written something ealready that could provide useful assistance then that would be great. Thanks guys
PHP has a wide variety of XML parsing functions. The most popular around here is the DOM. You can use DOM functions to find the specific XML tags you're interested in and retrieve their data.
Unfortunately you did not provide an example of the XML you're trying to work with, otherwise I'd post a brief example.
If you have to stay with xml format using php you use something with this here. If you can change the format to basic csv text you could try using wordpress plugin here.
Also php has a function for csv files called fgetcsv , so I would say get the information needed from your file.
Pass to a variable and than use wp_insert_post to create a post. Put it all in a while or foreach loop and should work fine - Or try the plug-in first.
As for duplicate content, perhaps you could pass the information in an array and then use array_unique to delete any duplicates (just off the top of my head theres probably a better way or function out there).

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