我期待从CrunchBase API中获取公司列表的数据,然后将该数据添加到数据库中的选定表中。我真的不知道从哪里开始。我一直在看cURL。如何获取API数据并使用PHP将其添加到数据库中?
我在哪里,在现在:
$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;
}
我想我现在应该分析它,然后将数据存储在数据库中。
我努力找到解析数据,代码如下:
$json_string = '<url.json>';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, true);
print_r($obj['Result']);
不幸的是,我不知道我在做什么等什么改变或在任何输入从这里将去非常感谢。
作为Cameeob2003指出的那样,你的'$ data'变量是不确定的。如果您的'error_reporting'设置为开发设置正确,您应该在屏幕上收到警告。考虑切换到一个报告未定义变量的IDE - NetBeans对此非常有用。 – halfer