4

I am looking to pull data for a list of companies from CrunchBase API, then add that data into select tables in a database. I really don't know where to start. I have been looking at cURL.

Where I am at right 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 am supposed to parse it now, then store the data in the database.

My efforts to find code on parsing the data is the following:

$json_string = '<url.json>';

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

Unfortunately, I don't know what I'm doing so any input on what to change or where to go from here will be very much appreciated.

1
  • As Cameeob2003 points out, your $data variable was undefined. You should have received warnings on-screen that this was the case, if your error_reporting settings are set correctly for development. Consider switching to an IDE that reports undefined variables too - NetBeans is great for that. Commented Nov 4, 2013 at 20:23

3 Answers 3

5

Much simpler - access the URL directly, and don't bother with 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);

I'm assuming here that the URL in your question is an API that is guaranteed to return JSON strings. I didn't understand the purpose of the str_replace in your first code sample.

Sign up to request clarification or add additional context in comments.

Comments

1

To answer your quesion based on the cURL method you provided you arent saving any data to the $data variable when you are running your curl function. This prevents you from having any return.

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

As you can see above before I call curl_close($ch); I am saving the return of curl_exec($ch); into $data. And now I get the text "Developer Inactive" on my screen (I don't have an API key myself I'm assuming). So now when I return the $data variable I can manipulate the return as I see fit.

1 Comment

Good spot, I didn't see that $data wasn't even set!
0

You can also try...

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.