0

Here's the info I'm trying to break up into a database. I'm going to be using this only for my own use to analyse statistics and all that. I have been manually doing it with Excel but I'd like to save myself some work in future.

URL IS: http://fantasy.premierleague.com/web/api/elements/537/

Any idea how to scrape that info or easily convert it to excel format? I know a bit of php and mysql, but nothing about JSON and very little about scraping (I tried messing with SIMPLE_HTML_DOM).

2
  • 1
    First, use jsbeautifier.org to make it human readable. it will make your life easier. Also, everyone is right, use json_decode and you will get back an array with data and more arrays. Take out what you want and create a csv file (en.wikipedia.org/wiki/Comma-separated_values and php.net/manual/en/function.fputcsv.php) Commented Dec 31, 2012 at 5:19
  • Wow. thanks for that. I saw a ton of JSON formatters, but it was still hard to read as it is a complicated one, but your link is a lot better. Thanks so much. Commented Dec 31, 2012 at 5:23

8 Answers 8

3

You can convert it into an array as

 $array = json_decode(file_get_contents('http://fantasy.premierleague.com/web/api/elements/537/'));

json_decode()

You could use PEAR excel writer to convert it into excel

Sign up to request clarification or add additional context in comments.

2 Comments

@shiplu.mokadd.im 'your data' does not mean a normal string. It is clear that I meant the JSON data
Thanks ravz. This looks like it'll get me what I want, and thanks also for the PEAR thingy... It looks very useful and exactly what I'll want to do once I get the database up and running.
2

You need to JSON_decode the data in PHP.

$obj = JSON_decode($mydata));
print_r($obj);

Extra information for you: http://php.net/manual/en/function.json-decode.php

2 Comments

Thanks so much. I thought it was complicated, but turns out to be simple.
You can also use var_dump($obj); to see the contents of $obj
2

PHP has a json parser function json_decode().

So:

  1. Use the file_get_contents() function to read the json content from the URL into a string.

  2. Use json_decode() to create a PHP structure representation.

  3. Use PEAR Spreadsheet_Excel_Writer module to create your excel spreadsheet.

Yeah. Easy as 1, 2, 3.

1 Comment

Yeah.. I wasn't aware of the JSON_DECODE() command even after googling, but that looks to be exactly what I'm looking to do. Thanks!
1
<?php

$x=json_decode(file_get_contents('http://fantasy.premierleague.com/web/api/elements/537/'));
print_r($x);//$x will contain all the values in an array format.

?>

Comments

1
<?php
// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$json= file_get_contents('http://fantasy.premierleague.com/web/api/elements/537/', false, $context);
$arr= json_decode($json);

$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($arr, array ($xml, 'addChild'));
print $xml->asXML();

1 Comment

Looks complicated but I'll check it out and see if it does what I need. Thanks a lot!
1

PHP makes it very easy:

$str = file_get_contents('http://fantasy.premierleague.com/web/api/elements/537/');
$jsonarray = json_decode($str, true);
var_dump($jsonarray);

Of course you will have to analyze the structure of the array and figure out how to decompose it to what you are actually looking for.

2 Comments

Thanks! Yeah the structure is complicated but now that I know how to break it up, I can figure out how to sort the data.
One other thing you can do, for KISS loading, is spit out a .csv file, which excel can easily load, as an alternative to some of the other more complicated suggestions like the pear library. With csv, all you really need to do is make sure you have double quotes around the strings, commas between the column values, and a CRLF at the end of each line.
1

Try using $obj = json_decode($jsonStr) after you receive your response string by running curl on the URL you mentioned in PHP. Then you can grab params from the json object like

$obj['paramName'];

Then you can do anything you want with the information including putting it into a database.

For simple MySQL interaction in php, check out MySQLConnector class.

http://jakesankey.com/blog/2011/12/php-mysql-helper-class/

Comments

1

use just json_decode and get the converted data like this edit

$arr = json_decode('your JSON data',true);

echo $arr['transfers_out'];  // output 490374  //for array
echo $arr->transfers_out;  // output 490374  //for stdClass

2 Comments

-1 Fatal error: Cannot use object of type stdClass as array
The array version will not work. you need to pass the second parameter of json_decode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.