我如何使用PHP和MySQL抓取JSON信息


How would I scrape this JSON info using PHP and MySQL?

这是我试图分解成一个数据库的信息。我只会用它来分析统计数据之类的。我一直在用Excel手工做,但我想在将来节省一些工作。

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

任何想法如何刮信息或容易地将其转换为excel格式?我懂一点php和mysql,但对JSON一无所知,对抓取也知之甚少(我试过用SIMPLE_HTML_DOM)。

需要在PHP中对数据进行JSON_decode。

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

给你的额外信息:http://php.net/manual/en/function.json-decode.php

可以将其转换为数组

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

json_decode ()

您可以使用PEAR excel writer将其转换为excel

PHP有一个json解析函数json_decode()。

:

  1. 使用file_get_contents()函数将URL中的json内容读取为字符串

  2. 使用json_decode()创建一个PHP结构表示

  3. 使用PEAR Spreadsheet_Excel_Writer模块创建excel电子表格

是的。

<?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.
?>
<?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();

PHP让它变得很简单:

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

当然,你必须分析数组的结构,并找出如何将其分解为你实际要寻找的内容。

尝试使用$obj = json_decode($jsonStr)通过运行curl在PHP中提到的URL收到响应字符串后。然后你可以从json对象中获取参数,比如

$ obj [' paramName '],

然后你可以对这些信息做任何你想做的事情,包括把它放入数据库。

在php中进行简单的MySQL交互,请查看MySQLConnector类。http://jakesankey.com/blog/2011/12/php-mysql-helper-class/

只使用json_decode,并获得这样的转换数据编辑

$arr = json_decode('your JSON data',true);
echo $arr['transfers_out'];  // output 490374  //for array
echo $arr->transfers_out;  // output 490374  //for stdClass