JSON web hook to MySQL via PHP
<p>I'm a starting with PHP and I have a web server that receives an incoming web hook in JSON format.</p>

<p>The web hook can come in 3 different structures based on the object. Each different format gets inserted in to a different table based on "type":.</p>

<p>The 3 formats for webhook are as follows.</p>

<p>1.</p>

<pre class="lang-html prettyprint-override"><code>{"message": 
   {"team_id": 3, 
    "place_points": 4000, 
    "last_modified": 1480297989459, 
    "latitude": 27.140134, 
    "guard_object_id": 130, 
    "enabled": true, 
    "place_id": "YjliYjRkYzJiZmI0NDIyZj"placezRlZmM0OGFhOWIxMTguMTY=",      
    "longitude": -80.192017}, 
    "type": "place"}
</code></pre>



<p>2.</p>

<pre class="lang-html prettyprint-override"><code>{"message": 
    {"bonus_expiration": null, 
     "store_id": "YjhlMWQ3MDcwN2EzNGMwY2I1NGE5YzVmNDA3ZWRlMGIuMTY=", 
     "enabled": true, "longitude": -80.197377, 
     "last_modified": 1474148043976, 
     "active_fort_modifier": null, 
     "latitude": 27.146217}, 
     "type": "store"}
</code></pre>



<p>3.</p>

<pre class="lang-html prettyprint-override"><code>{"message": 
    {"disappear_time": 1480353234, 
    "meeting_id": "88dedc26aad", 
    "move_2": null, 
    "time_until_hidden_ms": 1411382559, 
    "last_modified_time": 1480352334560, 
    "encounter_id": "MzIxNTQ1NzIzMzMzMDExOTI5Mg==", 
    "move_1": null, 
    "individual_defense": null, 
    "object_id": 13, 
    "individual_stamina": null, 
    "time_detail": -1, 
    "longitude": -80.19295513708012, 
    "latitude": 27.141183040207377, 
    "individual_attack": null}, 
    "type": "object"}
</code></pre>



<p>This is PHP I've put together, but its not working when im sending the webhook to the address of the PHP Script. Any help would be highly appreciated.</p>

<pre class="lang-html prettyprint-override"><code>&lt;?php


# MySQL database connection info
$host = 'localhost:3306';
$dbname = 'xxxxxxx';
$user = 'xxxxxxx';
$password = '-------';
$db = new PDO('mysql:host=' . $host . ';dbname=' . $dbname, $user, $password);

# Define Type Variables

$place = 'place';
$store = 'store';
$object = 'object';

# Decide if the WebHook is a place, store, or object

//$input = file_get_contents('mysql_webhook_payload.json'); # Local file for debugging
$input = file_get_contents('php://input'); # POST data from webhook
$payload = json_decode($input, true);

#IF LOOP for type

if ($payload['message']['type'] == $place ) {

  $team_id = $payload['message']['team_id'];
  $place_points = $payload['message']['place_points'];
  $last_modified = $payload['message']['last_modified'];
  $latitude = $payload['message']['latitude'];
  $guard_object_id = $payload['message']['guard_object_id'];      
  $enabled = $payload['message']['enabled']; 
  $place_id = $payload['message']['place_id'];      
  $longitude = $payload['message']['longitude'];
    $sql = "INSERT INTO places (`place_id`, `team_id`, `guard_object_id`, `place_points`, `enabled`, `latitude`, `longitude`, `last_modified`) 
            VALUE ($place_id, $team_id, $guard_object_id, $place_points, $enabled, $latitude, $longitude, $last_modified)";  
}
else if ($payload['message']['type'] == $store ) {
    $bonus_expiration = $payload['message']['bonus_expiration'];
    $store_id = $payload['message']['store_id'];
    $enabled = $payload['message']['enabled'];
    $longitude = $payload['message']['longitude'];
    $last_modified = $payload['message']['last_modified'];
    $active_fort_modifier = $payload['message']['bonus_expiration'];
    $latitude = $payload['message']['latitude'];

    ////missing sql entry


}
else if ($payload['message']['type'] == $object ) {
    $disappear_time = $payload['message']['disappear_time'];
    $meeting_id = $payload['message']['meeting_id'];
    $encounter_id = $payload['message']['encounter_id'];
    $longitude = $payload['message']['longitude'];
    $last_modified_time = $payload['message']['last_modified_time'];
    $move_1 = $payload['message']['move_1'];
    $latitude = $payload['message']['latitude'];
    $individual_defense = $payload['message']['individual_defense'];
    $object_id = $payload['message']['object_id'];
    $individual_stamina = $payload['message']['individual_stamina'];
    $individual_attack = $payload['message']['individual_attack'];
    $time_detail = $payload['message']['time_detail'];
    $move_2 = $payload['message']['move_2'];

    $sql = "INSERT INTO `object`(`encounter_id`, `meeting_id`, `object_id`, `latitude`, `longitude`, `disappear_time`, `individual_attack`, `individual_defense`, `individual_stamina`, `move_1`, `move_2`, `last_modified`, `time_detail`) VALUES ($encounter_id, $meeting_id, $object_id, $latitude, $longitude, $disappear_time, $individual_attack, $individual_defense, $individual_stamina, $move_1, $move_2, $last_modified, $time_detail)";

}

?&gt;
</code></pre>



<p>I am probably way off here</p>