How to pull API data and add it to the database with PHP?

I am looking for data for a list of companies from the CrunchBase API, and then add this data to the selection tables in the database. I really don't know where to start. I look at cURL.

Where am I now:

$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;
}

I think I should parse it now and then save the data in the database.

My efforts to find the code when analyzing the data are as follows:

$json_string = '<url.json>';

$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, true);
print_r($obj['Result']);

Unfortunately, I do not know what I am doing, so any contribution to what I need to change or where to go will be greatly appreciated.

+4
source share
3 answers

Much easier - directly access the URL and don’t worry about 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- - API, JSON. str_replace .

+5

cURL, , - $data, curl. .

Fixed

$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);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

, curl_close($ch);, curl_exec($ch); $data. " " ( API, ). , $data, , .

+1

...

header("Content-type: application/xml");
  $token="AUTHTOKEN";
  $url = "http://api.crunchbase.com/v/1/company/audible-coffee.js?api_key=API_KEY&callback=?";
  $param= "authtoken=".$token.";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  $result = curl_exec($ch);
  curl_close($ch);
  echo $result;
  return $result;
0

All Articles