如何使用php获取数组的特定内容(?)?

时间:2019-06-08 12:19:20

标签: php arrays json decode file-get-contents

所以我想我有一个包含来自API的很多数据的数组(虽然可能是json,但我不确定:https://bitebtc.com/api/v1/market),我想提取一些特定的数据,例如'percent '。

我尝试了三种使用json_decode()方法相对相似的方法,但是没有任何效果:/这是其中一种:

fucn <- function(years){
  file1 = str_replace_all(files, years, years)
  file2 = str_replace_all(files, years, as.character(as.numeric(years) + 1))
  return(file1, file2)
  dt_temp1 <- fread(file1, sep = ",")
  dt_temp2 <- fread(file2, sep = ",")
  return(dt_temp)
}

mylist <- lapply(years_to_process, fucn)

1 个答案:

答案 0 :(得分:0)

访问file_get_contents()的文档,以获取有关如何传递此api所需的标头(Content-Type: application/json)的信息。

curl -X GET 'https://bitebtc.com/api/v1/markets' -H 'Content-Type: application/json'

这可能会使您得到的东西变得相当不同(!)... 使用文档中的示例代码将其应用于您的情况,我们提出了类似这样的方法:

<?php
// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Content-Type: application/json\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('https://bitebtc.com/api/v1/markets', false, $context);

// now print out the results so you can see what you're working with (should be a huge JSON string)
print $file;

// decode it and var dump it to see how to work with it
$decoded = json_decode($file);
var_dump($decoded);

?>

您可能必须处理此示例;我不在安装PHP的计算机上进行测试...