我期待從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