如何使用PHP提取API数据并将其添加到数据库中?

jea*_*les 4 php json curl

我希望从CrunchBase API中提取公司列表的数据,然后将该数据添加到数据库中的选择表中.我真的不知道从哪里开始.我一直在看cURL.

我现在在哪里:

$url = "http://api.crunchbase.com/v/1/company/audible-coffee.js?api_key=API_KEY&callback=?";
$data = get_data($url);
// Note: ideally you should use DOM manipulation to inject the <base>
// tag inside the <head> section
$data = str_replace("<head>", "<head><base href=\"$url\">", $data);
echo $data;


function get_data($url) {
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  curl_close($ch);
  return $data;
}
Run Code Online (Sandbox Code Playgroud)

我想我现在应该解析它,然后将数据存储在数据库中.

我在解析数据时找到代码的努力如下:

$json_string = '<url.json>';

$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, true);
print_r($obj['Result']);
Run Code Online (Sandbox Code Playgroud)

不幸的是,我不知道自己在做什么,所以对于改变什么或从哪里去的任何意见都将非常感激.

hal*_*fer 5

更简单 - 直接访问URL,不要打扰cURL:

$url = "http://api.crunchbase.com/v/1/company/audible-coffee.js?api_key=API_KEY&callback=?";
$jsondata = file_get_contents($url);
$obj = json_decode($jsondata);
Run Code Online (Sandbox Code Playgroud)

我假设您的问题中的URL是一个保证返回JSON字符串的API.我不明白str_replace你的第一个代码示例中的目的.