Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending REST Call to Google Cloud PHPMyAdmin Server not working?

I have successfully deployed a phpMyAdmin server on Google Cloud by following this link. I am having trouble trying to write to a database that I made in phpMyAdmin. I am trying to create a Notification Service based on the new Firebase Cloud Messaging that Google has released.

NotificationInstanceService.java

public class NotificationInstanceService extends FirebaseInstanceIdService {
    private static final String TAG = "NotificationInstance";

    @Override
    public void onTokenRefresh() {

        //Getting registration token
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        //Displaying token on logcat
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        sendRegistrationToServer(refreshedToken);

    }

    private void sendRegistrationToServer(String token) {
        //You can implement this method to store the token on your server
        //Not required for current project
        OkHttpClient client = new OkHttpClient();
        //Create the request body
        RequestBody body = new FormBody.Builder().add("Token", token).build();
        //Know where to send the request to
        Request request = new Request.Builder().url("<db link>.appspot.com/fcm/register.php")
                .post(body)
                .build();
        //Create
        try {
            client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

And I deployed, on https://<db link>.appspot.com, a file under /fcm/register.php which looks like so:

register.php

<?php
    if (isset($_POST["Token"])) {

           $_uv_Token=$_POST["Token"];
           $conn = mysqli_connect("<db link>.appspot.com","root","","fcm") or die("Error connecting");
           $q="INSERT INTO users (Token) VALUES ( '$_uv_Token') "
              ." ON DUPLICATE KEY UPDATE Token = '$_uv_Token';";

      mysqli_query($conn,$q) or die(mysqli_error($conn));
      mysqli_close($conn);
    }
 ?>

I am confused because I don't seem to be writing anything to my database called users which I know I already created in the MySQL server created on phpMyAdmin. I know that the user name and password are also already set on register.php. Is there any way I can debug whether or not my script is actually going into the PHP code? How can I debug the PHP code? I also that the Request is actually being built as I can debug through that part of the code. Any help would be appreciated. Thanks!

EDIT: Some files that may be helpful that I created when trying to deploy my server:

app.yaml:

application: <app server url>
service: default
runtime: php55
api_version: 1
version: alpha-001

handlers:

- url: /(.+\.(ico|jpg|png|gif))$
  static_files: \1
  upload: (.+\.(ico|jpg|png|gif))$
  application_readable: true

- url: /(.+\.(htm|html|css|js))$
  static_files: \1
  upload: (.+\.(htm|html|css|js))$
  application_readable: true

- url: /(.+\.php)$
  script: \1
  login: admin

- url: /.*
  script: index.php
  login: admin

- url: /.*
  script: register.php
  login: admin

config.inc.php:

<?php 
$cfg['blowfish_secret'] = '<Secret>'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

/*
 * Servers configuration
 */
$i = 0;

// Change this to use the project and instance that you've created.
$host = '/cloudsql/<app server url>:us-central1:<database name>-app-php';
$type = 'socket';

/*
* First server
*/
$i++;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['socket'] = $host;
$cfg['Servers'][$i]['connect_type'] = $type;
$cfg['Servers'][$i]['compress'] = false;
/* Select mysql if your server does not have mysqli */
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
/*
 * End of servers configuration
 */

/*
 * Directories for saving/loading files from server
 */
$cfg['UploadDir'] = '';
$cfg['SaveDir'] = '';

$cfg['PmaNoRelation_DisableWarning'] = true;
$cfg['ExecTimeLimit'] = 60;
$cfg['CheckConfigurationPermissions'] = false;
// [END all]

php.ini:

google_app_engine.enable_functions = "php_uname, getmypid"

EDIT: Text when in browser, going to .appspot.com/fcm/register.php

array(11) { ["pmaCookieVer"]=> string(1) "5" ["pma_lang"]=> string(2) "en" ["pma_collation_connection"]=> string(15) "utf8_unicode_ci" ["pma_console_height"]=> string(2) "92" ["SACSID"]=> string(355) "~AJKiYcFgym76QZfbMX35ddCTdKKf-O7q5koLvNZ0coWTMvw9aNlR5fusNyLRzFyw5DB_t2ygVuTEjHwgrgBco4-wr_V3Eer_Mf0CDuGX2e4IpirCNeiGxkRvaLgRPPyZNZWKUx1mF_DChjsksTirkY5WCzlA3G3MO9bBScrLw8kNOFGnvzkev3-B2x31s_TmnDN5aJ0G3-nPueI4FPpKaaMlPsITziccvXMpiehglQOKoo1Bol3EZSF1tjI9QoJuc-6X_sHgJ0IEppg7K-cBapaEx5CmDD2kWOggnVPWnGj1SiKFUnE3DZD46bjovf5me7IdwfVX22bv5D2PJDPQEN4m3D7yP3-Wdg" ["pma_console_config"]=> string(103) "{"alwaysExpand":false,"startHistory":false,"currentQuery":true,"enterExecutes":false,"darkTheme":false}" ["pma_console_mode"]=> string(4) "show" ["phpMyAdmin"]=> string(40) "cfd814e10982d138c7ed4a3ef510c454c0e5f9b9" ["pma_iv-1"]=> string(24) "bSPnJOOBe5x0iXPbbU5Nww==" ["pmaUser-1"]=> string(24) "oHSLKZ7q6eOaXJ475Q6tzw==" ["pmaPass-1"]=> string(24) "dwKZ9gQPCoe/Uk4sWS4s2g==" }

new register.php:

<?php
    if (isset($_REQUEST["Token"])) {

           $_uv_Token=$_REQUEST["Token"];
           $conn = mysqli_connect("/cloudsql/<Database ServerURL>","root","","FCM") or die("Error connecting");
           $q="INSERT INTO users (Token) VALUES ( '$_uv_Token') "
              ." ON DUPLICATE KEY UPDATE Token = '$_uv_Token';";
      var_dump(mysqli_query($conn,$q));
      mysqli_query($conn,$q) or die(mysqli_error($conn));
      mysqli_close($conn);
    } else {
        var_dump($_REQUEST);
    }
 ?>

NotificationInstanceService.java

New POST Request:

    Request request = new Request.Builder().url("<Application Server>/fcm/register.php?Token=123")
            .post(body)
            .build();
like image 818
user1871869 Avatar asked Aug 21 '16 17:08

user1871869


1 Answers

I don't know what exactly is your question, it's not clear. but since you asked: Is there any way I can debug whether or not my script is actually going into the PHP code? How can I debug the PHP code? here is your answer:

Change your php code to this:

<?php
    if (isset($_POST["Token"])) {

           $_uv_Token=$_POST["Token"];
           $conn = mysqli_connect("<db link>.appspot.com","root","","fcm") or die("Error connecting");
           $q="INSERT INTO users (`Token`) VALUES ( `{$_uv_Token}`) "
              ." ON DUPLICATE KEY UPDATE `Token` = `{$_uv_Token}`;";

      mysqli_query($conn,$q) or die(mysqli_error($conn));
      mysqli_close($conn);
    } else {
        echo "Token is not set!";
    }
?>

then in NotificationInstanceService.java check the response of OkHttpClient call:

...
    try {
        Response response = client.newCall(request).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (!response.isSuccessful()){
        Log.w(TAG, "Unexpected response"+response.toString());
    } else {
        Log.w(TAG, "response: "+response.body().string());
    }
...
like image 62
M D P Avatar answered Nov 14 '22 12:11

M D P