我希望从 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;
}
我想我现在应该解析它,然后将数据存储在数据库中。
我努力寻找解析数据的代码如下:
$json_string = '<url.json>';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, true);
print_r($obj['Result']);
不幸的是,我不知道我在做什么,因此非常感谢您对更改内容或从哪里开始的任何意见。
最佳答案
更简单 - 直接访问 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);
我在这里假设您问题中的 URL 是一个保证返回 JSON 字符串的 API。我不明白您的第一个代码示例中
str_replace
的用途。关于php - 如何使用PHP提取API数据并将其添加到数据库中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19735318/