Skip to content

Instantly share code, notes, and snippets.

@CircleSquaredPublishing
Created April 19, 2017 04:19
Show Gist options
  • Select an option

  • Save CircleSquaredPublishing/25c0f768d0fdfaf382e5175e61dac38a to your computer and use it in GitHub Desktop.

Select an option

Save CircleSquaredPublishing/25c0f768d0fdfaf382e5175e61dac38a to your computer and use it in GitHub Desktop.

Revisions

  1. CSQ2 created this gist Apr 19, 2017.
    170 changes: 170 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,170 @@
    <?php
    /**
    * @package Heatery
    * @version 2.0 [April 18, 2017]
    * @author Will Conkwright
    * @copyright Copyright (c) 2017 Circle Squared Publishing, LLC
    * @license Licensed MIT
    *
    * Permission is hereby granted, free of charge, to any person obtaining a copy
    * of this software and associated documentation files (the "Software"), to deal
    * in the Software without restriction, including without limitation the rights
    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    * copies of the Software, and to permit persons to whom the Software is
    * furnished to do so, subject to the following conditions:
    *
    * The above copyright notice and this permission notice shall be included in
    * all copies or substantial portions of the Software.
    *
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    * THE SOFTWARE.
    */


    $address = $_POST['address'];

    if(!$address){
    $db = connect();
    $latitude = '35.0310';
    $longitude = '-76.6930';
    $api_results = get_api_results( $latitude, $longitude );
    $insert_api_results = insert_api_results( $db, $api_results );
    $select_api_results = select_api_results( $db, $latitude, $longitude );
    } else {
    $db = connect();
    $geo_var = geocode($address);
    $latitude = $geo_var[0];
    $longitude = $geo_var[1];
    $city = $geo_var[2];
    $api_results = get_api_results( $latitude, $longitude );
    $insert_api_results = insert_api_results( $db, $api_results );
    $select_api_results = select_api_results( $db, $latitude, $longitude );
    }



    /**
    * Remote connection.
    */

    function connect() {
    require ( '//home/heatery/credentials/connect.php' );
    $db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    return $db;
    }

    function geocode( $address ){
    $data_array = array();
    $address = urlencode( $address );
    $url = "https://maps.google.com/maps/api/geocode/json?&address={$address}";
    $response = file_get_contents( $url );
    $results = json_decode($response, true);
    $lat = $results['results'][0]['geometry']['location']['lat'];
    $long = $results['results'][0]['geometry']['location']['lng'];
    $geo_city = $results['results'][0]['address_components'][0]['long_name'];
    array_push( $data_array, $lat, $long, $geo_city );
    return $data_array;
    }

    function get_api_results( $latitude, $longitude ){
    $url = 'https://graph.facebook.com/v2.8/search?q=&type=place&distance=3200&center='.$latitude.','.$longitude.'&fields=location,name,talking_about_count,were_here_count,description,website,cover,about&limit=250&access_token='.HEATERY_API_KEY;
    $table = basename( __FILE__ , '.php' );
    $name = ( './inc/data/' . $table . '.json' );
    $file = fopen( $name, 'w' );
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_FILE, $file );
    curl_exec( $ch );
    curl_close( $ch );
    $response = file_get_contents( $name );
    $results = json_decode( $response, true );
    return $results;
    }

    function insert_api_results( $db, $api_results ) {
    $sql = ("INSERT INTO top10_markers(
    FID,
    fb_web,
    fb_cover,
    fb_about,
    fb_description,
    fb_name,
    fb_likes,
    fb_were_here,
    fb_talking_about,
    fb_lat,
    fb_lng)
    VALUES
    (?,?,?,?,?,?,?,?,?,?,?)");
    $stmt1 = $db->prepare( $sql );
    $stmt1->bind_param("dsssssiiidd",
    $FID,
    $fb_web,
    $fb_cover,
    $fb_about,
    $fb_description,
    $fb_name,
    $fb_likes,
    $fb_were_here,
    $fb_talking_about,
    $fb_lat,
    $fb_lng);

    foreach ( $api_results['data'] as $k=>$v) {
    $FID = mysqli_real_escape_string($db, $v['id']);
    $fb_web = mysqli_real_escape_string($db, $v['website']);
    $fb_cover = mysqli_real_escape_string($db, $v['cover']['source']);
    $fb_about = mysqli_real_escape_string($db, $v['about']);
    $fb_description = mysqli_real_escape_string($db, $v['description']);
    $fb_name = mysqli_real_escape_string($db, $v['name']);
    $fb_likes = mysqli_real_escape_string($db, $v['likes']);
    $fb_were_here = mysqli_real_escape_string($db, $v['were_here_count']);
    $fb_talking_about = mysqli_real_escape_string($db, $v['talking_about_count']);
    $fb_lat = mysqli_real_escape_string($db, $v['location']['latitude']);
    $fb_lng = mysqli_real_escape_string($db, $v['location']['longitude']);
    $stmt1->execute();
    }
    $stmt1->close();
    }

    function select_api_results( $db, $latitude, $longitude ) {
    $stmt = "SELECT
    FID,
    fb_web,
    fb_name,
    fb_lat,
    fb_lng,
    fb_talking_about,
    TRUNCATE(fb_lat,3) AS fb_lat,
    TRUNCATE (fb_lng,3) AS fb_lng,
    TRUNCATE((SQRT(POW(69.1 * (fb_lat - $latitude),2) + POW(69.1 *( $longitude - fb_lng) * COS(fb_lat/57.3), 2)) * 0.621371),2)
    AS fb_distance
    FROM top10_markers
    WHERE fb_date = curdate()
    AND
    fb_talking_about > 0
    HAVING fb_distance < 2
    ORDER BY fb_talking_about DESC LIMIT 20";

    if ( $result = $db->query( $stmt ) ) {
    $json_results = array();
    $c = 0;
    while ( $obj = $result->fetch_object() ) {
    $json_results[$c] = $obj;
    $name = ( './inc/data/results.json' );
    $fp = fopen( $name, 'w' );
    fwrite( $fp, json_encode( $json_results ) );
    fclose( $fp );
    ++$c;
    }
    $result->close();
    }
    $db->close();
    }

    ?>