【发布时间】:2016-12-28 14:09:54
【问题描述】:
我无法连接到托管 MySQL 数据库的 Google Cloud PHP 服务器。这是我向我的 PHP 服务器发送通知的代码。
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("<app server url>/register.php")
.post(body)
.build();
//Create
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这似乎正确地通过并且似乎没有抛出任何堆栈跟踪。然后,当我部署我的 PHP 服务器时,我创建了以下文件:
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"
最后,register.php 这是我的 php 脚本,位于所有这些文件的当前目录中:
注册.php:
<?php
function dbg($data){
file_put_contents(__DIR__.'/log.txt',$data.PHP_EOL,FILE_APPEND );
}
$conn = mysql_connect(':/cloudsql/<app server url>:us-central1:<database name>',
'root', // username
'' // password
);
if (isset($conn) && isset($_POST["Token"])) {
$_uv_Token=$_POST["Token"];
echo $conn;
$q="INSERT INTO users (Token) VALUES ( '$_uv_Token') "
." ON DUPLICATE KEY UPDATE Token = '$_uv_Token';";
$result = mysqli_query($conn,$q) or die(mysqli_error($conn));
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Inserted successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
dbg($q); /* where $q is the sql */
dbg(print_r($result,true)); /* config */
mysqli_close($conn);
}
?>
我似乎无法找到我的问题所在。我似乎无法调试或找到任何错误日志,说明我是否连接到错误的数据库,或者我的 REST 调用是否因某种原因而被截获。似乎在客户端,在NotificationInstanceService.java 中,注册令牌被发送到服务器,但服务器实际上从未在其中存储 id 或令牌。我很确定我的应用程序服务器的所有 URL 都已正确配置。我尝试$echo 所有的回复,我得到了这样的回复,但似乎找不到在哪里获得这些$echo 声明。任何帮助将非常感激。谢谢!
【问题讨论】:
标签: php android google-app-engine phpmyadmin google-cloud-sql