Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts

    API (Grab data and place into database)

    If I want to grab data from an API, how can I do this?

    I'm given a URL with parameters like a secret key, and the page renders this:


    What would I do to import this data to a MySQL database? I understand the querying to place the info in, I'm confused as to how I can grab the information off of this page. (Again, it's just a URL, and it's not on my server)

    I can change the format that it outputs, but I felt like PHP may be the easier one?

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    this is just a multi-dimensional array..

    read this: http://php.net/manual/en/language.types.array.php
    and this: http://php.net/manual/en/control-structures.foreach.php

    pretty straight forward.

  • #3
    New Coder
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I know what it is, but how do I grab it off of an external server? How do I physically bring it over to my code?

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ah ok, well you could use CURL, something like this:

    PHP Code:
                $ch curl_init("http://www.SomeUrl.com/Server.php");
                
    curl_setopt($chCURLOPT_RETURNTRANSFER,1); // forces response into return string, not echo
                
    curl_setopt($chCURLOPT_HEADER0);
                
    $Response curl_exec($ch);
                
    curl_close($ch);
                
                echo  
    $Response

  • Users who have thanked angst for this post:

    Blockis (04-19-2012)

  • #5
    New Coder
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts
    You are THE MAN

    So how would I start the importing to my database? Is it weird that the array isn't associated as a variable? (Just looking at examples from the PHP.net link, most of the examples are with $var = array where what I'm given just starts off with array(...))

    It makes more sense to connect to the database before doing the foreach's right?

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    yes, connect to the database before you start the loops. you'll need to use a few loops to make this happen.

    once you get to the array with the data you want, insert the values. this looks like a transaction response. normally I would make a table in the database for it, then make each field with the same names as those return. this way you would have a dynamic insert, something like this;

    PHP Code:
    $Insert "";
    foreach (
    $arr as $key => $value) {
       
    $Insert .= trim($key) . " = '" mysql_real_escape_string(trim($value)) . "'";
    }
    mysql_query("INSERT INTO YourTable SET " rtrim($Insert",") ); 

  • #7
    New Coder
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts
    But how do I get through the first array, and extract the
    PHP Code:
    'data' => array 
    array?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •