Jump to content

Recommended Posts

Hi everyone, I am an ASP guy who is trying to make the change over to PHP.  I am playing around with the Crunchbase API for my first PHP attempt, utilizing JSON and outputting values from a JSON result set.

 

I am able to return top level array items, but I am not sure how to go about returning nested array results.  Below is the code I am using, and the JSON text I am using...let me know if anyone has any ideas, or if there is an easier way than how I am approaching it.

 

If you look at the JSON data, I am trying to figure out how to print out Greylock, Meritech, and Founders Fund (the "financial_org" elements).  Thanks in advance for any help!!!

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
  <head>
    <title>test page</title>
  </head>
  <body>
    <?php
  
$jsonText = @file_get_contents($Url);

$jsonObject = json_decode($jsonText);

$companyName = $jsonObject->{"name"};
$companyURL = $jsonObject->{"homepage_url"};
$fundingObject = $jsonObject->{"funding_rounds"};
$fundingRound = $fundingObject[2]->{"round_code"};
$fundingAmount = $fundingObject[2]->{"raised_amount"};
$fundingYear = $fundingObject[2]->{"funded_year"};
$fundingMonth = $fundingObject[2]->{"funded_month"};
$fundingDay = $fundingObject[2]->{"funded_day"};

print "name = $companyName<br>";
print "url = $companyURL<br>";
print "funding round = $fundingRound<br>";
print "funding Amount = $fundingAmount<br>";
print "funding date = $fundingMonth/$fundingDay/$fundingYear<br>";
    ?>
  </body>
</html>

 

JSON DATA

{"name": "Facebook",
"permalink": "facebook",
"crunchbase_url": "http://www.crunchbase.com/company/facebook",
"homepage_url": "http://facebook.com",
"blog_url": "http://blog.facebook.com",
"blog_feed_url": "http://blog.facebook.com/atom.php",
"category_code": "network_hosting",
"number_of_employees": 700,
"founded_year": 2004,
"founded_month": 2,
"founded_day": 1,
"deadpooled_year": null,
"deadpooled_month": null,
"deadpooled_day": null,
"deadpooled_url": "",
"tag_list": "facebook, college, students, profiles, network, online-communities, social-networking",
"alias_list": "",
"email_address": "",
"phone_number": "",
"description": "Social network",
"created_at": "Fri May 25 21:22:15 UTC 2007",
"updated_at": "Wed Apr 01 20:18:33 UTC 2009",
"funding_rounds":
  [{"round_code": "angel",
    "source_url": "",
    "source_description": "",
    "raised_amount": 500000.0,
    "raised_currency_code": "USD",
    "funded_year": 2004,
    "funded_month": 9,
    "funded_day": 1,
    "investments":
     [{"company": null,
       "financial_org": null,
       "person":
        {"first_name": "Peter",
         "last_name": "Thiel",
         "permalink": "peter-thiel"}}]},
   {"round_code": "a",
    "source_url": "http://www.techcrunch.com/2007/11/02/jim-breyer-extra-500-million-round-for-facebook-a-total-fiction/",
    "source_description": "Jim Breyer: Extra $500 Million Round For Facebook A \u201cTotal Fiction\u201d",
    "raised_amount": 12700000.0,
    "raised_currency_code": "USD",
    "funded_year": 2005,
    "funded_month": 5,
    "funded_day": 1,
    "investments":
     [{"company": null,
       "financial_org":
        {"name": "Accel Partners",
         "permalink": "accel-partners"},
       "person": null}]},
   {"round_code": "b",
    "source_url": "http://www.facebook.com/press/info.php?factsheet",
    "source_description": "Facebook Funding",
    "raised_amount": 27500000.0,
    "raised_currency_code": "USD",
    "funded_year": 2006,
    "funded_month": 4,
    "funded_day": 1,
    "investments":
     [{"company": null,
       "financial_org":
        {"name": "Greylock",
         "permalink": "greylock"},
       "person": null},
      {"company": null,
       "financial_org":
        {"name": "Meritech Capital Partners",
         "permalink": "meritech-capital-partners"},
       "person": null},
      {"company": null,
       "financial_org":
        {"name": "The Founders Fund",
         "permalink": "founders-fund"},
       "person": null}]},
   {"round_code": "c",
    "source_url": "http://www.techcrunch.com/2007/10/24/liveblogging-the-facebook-press-conference/#more-10260",
    "source_description": "Liveblogging  Press Conference",
    "raised_amount": 240000000.0,
    "raised_currency_code": "USD",
    "funded_year": 2007,
    "funded_month": 10,
    "funded_day": 1,
    "investments":
     [{"company":
        {"name": "Microsoft",
         "permalink": "microsoft"},
       "financial_org": null,
       "person": null}]},
   {"round_code": "c",
    "source_url": "http://venturebeat.com/2007/11/30/facebook-gets-60m-from-hong-kong-billionaire-li-ka-shing/",
    "source_description": "Facebook gets $60M from Hong Kong billionaire Li Ka-shing",
    "raised_amount": 60000000.0,
    "raised_currency_code": "USD",
    "funded_year": 2007,
    "funded_month": 11,
    "funded_day": 30,
    "investments":
     [{"company": null,
       "financial_org": null,
       "person":
        {"first_name": "Li",
         "last_name": "Ka-shing",
         "permalink": "li-ka-shing"}}]},
   {"round_code": "c",
    "source_url": "http://www.techcrunch.com/2008/01/15/breaking-samwer-brothers-invest-in-facebook/",
    "source_description": "",
    "raised_amount": 15000000.0,
    "raised_currency_code": "USD",
    "funded_year": 2008,
    "funded_month": 1,
    "funded_day": 15,
    "investments":
     [{"company": null,
       "financial_org":
        {"name": "European Founders Fund",
         "permalink": "european-founders-fund"},
       "person": null}]},
   {"round_code": "debt_round",
    "source_url": "http://www.businessweek.com/technology/content/may2008/tc2008059_855064.htm",
    "source_description": "",
    "raised_amount": 100000000.0,
    "raised_currency_code": "USD",
    "funded_year": 2008,
    "funded_month": 5,
    "funded_day": null,
    "investments":
     [{"company": null,
       "financial_org":
        {"name": "TriplePoint Capital",
         "permalink": "triplepoint-capital"},
       "person": null}]},
   {"round_code": "c",
    "source_url": "http://www.paidcontent.org/entry/419-facebook-investor-li-ka-shing-increasing-stake-in-facebook/",
    "source_description": "Updated: Facebook Investor Li Ka-Shing Upping Stake To $120 Million: Report",
    "raised_amount": 60000000.0,
    "raised_currency_code": "USD",
    "funded_year": 2008,
    "funded_month": 3,
    "funded_day": 27,
    "investments":
     [{"company": null,
       "financial_org": null,
       "person":
        {"first_name": "Li",
         "last_name": "Ka-shing",
         "permalink": "li-ka-shing"}}]}],
"screenshots":
  [],
"external_links":
  [{"external_url": "http://latimesblogs.latimes.com/technology/2008/09/facebook-hire-1.html",
    "title": "Facebook hires general counsel as it continues to grow"}]}

 

(changed

to


tags)

try this

$financial_org = $fundingObject[3]->{"round_code"}->{"investments"}->{"financial_org"};
foreach($financial_org as $FO)
{
$name = $FO->{"name"};
$permalink = $FO->{"permalink"};
echo "$name -> $permalink<br>";
}

 

try this

$financial_org = $fundingObject[3]->{"round_code"}->{"investments"}->{"financial_org"};
foreach($financial_org as $FO)
{
$name = $FO->{"name"};
$permalink = $FO->{"permalink"};
echo "$name -> $permalink<br>";
}

 

Thanks for the response!  I am using EasyPHP, and the addition of this line results in an error - output from browser below:

 

Notice: Trying to get property of non-object in C:\Program Files\EasyPHP 3.0\home\test\cbtest.php on line 22

 

Notice: Trying to get property of non-object in C:\Program Files\EasyPHP 3.0\home\test\cbtest.php on line 22

name = Facebook

url = http://facebook.com

funding round = b

funding Amount = 27500000

funding date = 4/1/2006

 

Warning: Invalid argument supplied for foreach() in C:\Program Files\EasyPHP 3.0\home\test\cbtest.php on line 30

 

 

Current code, with additions.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
  <head>
    <title>test page</title>
  </head>
  <body>
    <?php
  
$jsonText = @file_get_contents($url);

$jsonObject = json_decode($jsonText);

$companyName = $jsonObject->{"name"};
$companyURL = $jsonObject->{"homepage_url"};
$fundingObject = $jsonObject->{"funding_rounds"};
$fundingRound = $fundingObject[2]->{"round_code"};
$fundingAmount = $fundingObject[2]->{"raised_amount"};
$fundingYear = $fundingObject[2]->{"funded_year"};
$fundingMonth = $fundingObject[2]->{"funded_month"};
$fundingDay = $fundingObject[2]->{"funded_day"};

$financial_org = $fundingObject[2]->{"round_code"}->{"investments"}->{"financial_org"};

print "name = $companyName<br>";
print "url = $companyURL<br>";
print "funding round = $fundingRound<br>";
print "funding Amount = $fundingAmount<br>";
print "funding date = $fundingMonth/$fundingDay/$fundingYear<br>";

foreach($financial_org as $FO)
{
$name = $FO->{"name"};
$permalink = $FO->{"permalink"};
echo "$name -> $permalink<br>";
}
    ?>
  </body>
</html>


This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.